所以我有一个旧的angular 2-rc4项目,它运行在asp.net web api 4上,我正在尝试实现文件上传,但我在从前端发布文件时遇到了问题。
我得到错误unexpected end of mime multipart stream
这种情况只发生在客户端项目中,如果我从邮递员那里发送请求,文件上传成功,有什么想法会导致这种情况发生吗?
到目前为止我尝试了什么
我读到我们可能需要在流的末尾附加一个CRLF,所以实现它,这仍然没有解决问题
reqStream.Position = 0;
MemoryStream tempStream = new MemoryStream();
reqStream.CopyTo(tempStream);
tempStream.Seek(0, SeekOrigin.End);
StreamWriter writer = new StreamWriter(tempStream);
writer.WriteLine();
writer.Flush();
tempStream.Position = 0;
StreamContent streamContent = new StreamContent(tempStream);
foreach (var header in Request.Content.Headers)
{
streamContent.Headers.Add(header.Key, header.Value);
}
这是发送请求的前端代码
let body = JSON.stringify(jobFilesUpload);
const fileHeaders = new Headers();
fileHeaders.append('Accept', '*/*');
fileHeaders.append('Access-Control-Allow-Origin', '*');
fileHeaders.append('Content-Type', 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW');
this.authHttp.post(this.authService.apiUrl + "api/upload" + jobFilesUpload.OBPJobId, body, { headers: fileHeaders }).subscribe(do more stuff here)
更改了我调用端点的方式以使用XHR
var fd = new FormData();
for (var i in this.FilesList) {
fd.append("fileToUpload", this.FilesList[i], this.FilesList[i].name);
this.FilesList[i].Comments = this.FileComments;
}
let xhr = new XMLHttpRequest();
let url = this.authService.apiUrl + "api/SaveOBPJobFiles";
xhr.open("POST", url, true);
xhr.setRequestHeader("Authorization", "Bearer " + this.authService.userToken.JWT);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
// Display success or failure to user
var response = JSON.parse(xhr.response);
if (response.success == true) {
self.Item.OBPJobFiles = response.obpJobFiles;
self.Item.FileCount = response.obpJobFiles.length;
self.ClearOBPFiles();
console.log("File Success");
console.log(response.productionFiles);
} else {
self.toastrService.Error(response.message);
}
self.OBPFileProcessing = false;
setTimeout(() => {
self.systemService.StopLadda();
}, 1);
} else {
self.OBPFileProcessing = false;
setTimeout(() => {
self.systemService.StopLadda();
}, 1);
}
}
xhr.send(fd);
似乎为其他苦苦挣扎的人解决了最初的问题。