0
ASP.NET Web Api無法正確讀取流,錯誤消息是:「MIME多部分流的意外結束。MIME多部分消息未完成。angular-file-upload:無法上傳帶有數據的文件
這是我的角上傳代碼:
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控制器調用的,在那裏我將表單數據作爲參數。
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。
沒有,同樣的異常。我認爲這是如何angular-file-upload創建這種類型的請求。我總是得到控制器中的folderName數據,但無法讀取文件流 – vpetrovic
我不明白.net代碼,但我認爲你在服務器端出錯了。請搜索 - 「在asp.net中的http post上獲取正文」,這可能會對您有所幫助。 –