[英]Unable to receive form data in web api c#
I am trying to send the uploaded excel file to web api using XMLHttpRequest but all I receive in web api is an object. 我正在尝试使用XMLHttpRequest将上传的excel文件发送到Web api,但是我在Web api中收到的只是一个对象。 How to receive uploaded file in 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 网络API
[Route("massupload")]
[HttpPost]
public HttpResponseMessage MassUpload([FromUri] dynamic uploads)
{
try
{
response = Request.CreateResponse(HttpStatusCode.OK, "");
}
catch (Exception exception)
{
}
return response;
}
You can read the uploaded file from Request.Files
您可以从
Request.Files
读取上传的文件
foreach (string fileId in Request.Files)
{
HttpPostedFileBase file = Request.Files[fileId] as HttpPostedFileBase;
}
Also, you have decorated your uploads
parameter with FromUri
. 此外,您还使用
FromUri
装饰了uploads
参数。 You could also change that to FromBody
as your file is in your POST body, not the URI. 您还可以将其更改为
FromBody
因为文件位于POST正文中,而不是URI中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.