我正在尝试使用XMLHttpRequest将上传的excel文件发送到web api,但我在web api中收到的只是一个对象。如何在web api中接收上传的文件?
upload(file: File): void {
let formData: FormData = new FormData(),
xhr: XMLHttpRequest = new XMLHttpRequest();
formData.append("uploads", file, file.name);
xhr.open('POST', this._expenseServiceUrl + 'expenses' + '/' + 'massupload', true);
xhr.send(formData);
}
Web API
[Route("massupload")]
[HttpPost]
public HttpResponseMessage MassUpload([FromUri] dynamic uploads)
{
try
{
response = Request.CreateResponse(HttpStatusCode.OK, "");
}
catch (Exception exception)
{
}
return response;
}
答案 0 :(得分:0)
您可以从Request.Files
foreach (string fileId in Request.Files)
{
HttpPostedFileBase file = Request.Files[fileId] as HttpPostedFileBase;
}
此外,您已使用uploads
修饰了FromUri
参数。您也可以将其更改为FromBody
,因为您的文件位于POST正文中,而不是URI。