无法在Web API中接收表单数据#

时间:2016-08-31 01:20:40

标签: c# angular typescript

我正在尝试使用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;
        }

1 个答案:

答案 0 :(得分:0)

您可以从Request.Files

阅读上传的文件
foreach (string fileId in Request.Files)
{
   HttpPostedFileBase file = Request.Files[fileId] as HttpPostedFileBase;
}

此外,您已使用uploads修饰了FromUri参数。您也可以将其更改为FromBody,因为您的文件位于POST正文中,而不是URI。