angular-file-upload:无法上传带有数据的文件

时间:2017-09-26 06:06:00

标签: angular asp.net-web-api angular-file-upload

ASP.NET Web Api无法正确读取流,错误消息为:" MIME多部分流的意外结束。 MIME多部分邮件不完整。"

这是我的角度上传代码:

    vm.fileUploader = new FileUploader({
    url: 'api/FileAPI/Upload', withCredentials: true, method: 'POST',             
});

vm.fileUploader.onCompleteItem = function (fileItem, response, status, headers) {
    vm.data = response;
};

vm.fileUploader.onBeforeUploadItem = function (item) {
    item.formData = [{ folder: vm.folderName }];
}

这是我读取流的地方以及抛出异常的位置。从api控制器调用此方法,我将form-data作为参数。

    public async Task<IEnumerable<PhotoViewModel>> AddFile(HttpRequestMessage request)
{
    try
    {
        //nothing special in PhotoMultipartFormDataStreamProvider
        var provider = new PhotoMultipartFormDataStreamProvider(this.workingFolder);
        await request.Content.ReadAsMultipartAsync(provider);
        var photos = new List<PhotoViewModel>();
        foreach (var file in provider.FileData)
        {
            var fileInfo = new FileInfo(file.LocalFileName);
            photos.Add(new PhotoViewModel
            {
                Name = fileInfo.Name,
                Size = fileInfo.Length / 1024
            });
        }
        return photos;
    }
    catch(Exception ex)
    {
        throw new Exception("Error uploading file", ex);
    }
}

这是我的请求字符串:

Method: POST, RequestUri: 'http://localhost:49645/api/FileAPI/Upload', Version: 1.1, Content: System.Web.Http.WebHost.HttpControllerHandler+LazyStreamContent, Headers:
{
  Connection: keep-alive
  Accept: */*
  Accept-Encoding: gzip
  Accept-Encoding: deflate
  Accept-Encoding: br
  Accept-Language: sr-RS
  Accept-Language: sr; q=0.8
  Accept-Language: en-US; q=0.6
  Accept-Language: en; q=0.4
  Host: localhost:49645
  Referer: http://localhost:49645/upload
  User-Agent: Mozilla/5.0
  User-Agent: (Windows NT 10.0; Win64; x64)
  User-Agent: AppleWebKit/537.36
  User-Agent: (KHTML, like Gecko)
  User-Agent: Chrome/60.0.3112.113
  User-Agent: Safari/537.36
  Origin: http://localhost:49645
  Content-Length: 791882
  Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryxUFnwtUwHSuA4X5Q
}

当我上传没有附加数据的文件时,它工作正常,但是当我添加表单数据时,我得到了这个例外。我试图更改标题,内容类型,但没有运气。我也尝试过从其他问题中读到的东西,但没有使用angular-file-upload。

1 个答案:

答案 0 :(得分:0)

onBeforeUploadItem代码中

尝试此操作
vm.fileUploader.onBeforeUploadItem = function (item) {
    item.formData.push({ folder: vm.folderName });
}