如何使用 ajax 在 ASP.NET5 中上传文件?

How can I upload files in ASP.NET5 using ajax?

我读过很多关于使用传统 html 表单上传文件和在服务器端使用 IFormFile (ASP.NET5 RC) 的帖子。但是,我想使用 ajax(特别是 ng-file-upload)上传文件,而 IFormFile 在使用 ajax 时似乎不起作用。请求负载如下所示:

------WebKitFormBoundaryqGdWTqE8FFBpVPbA
Content-Disposition: form-data; name="file[0]"; filename="mydoc.pdf"
Content-Type: application/pdf


------WebKitFormBoundaryqGdWTqE8FFBpVPbA--

我相信这些文件正在 Request.Body 中传递,但我不确定如何从那里提取它们。

我刚刚使用 Kendo UI 上传控件完成了此操作,我还发现未填充 IFormFile 参数。我使用 Request.Form.Files

让它工作
 foreach (var file in Request.Form.Files)
        {
                var fileBytes = new byte[file.Length];

                await file.OpenReadStream().ReadAsync(fileBytes, 0, (int)file.Length);

                SaveDoc(fileBytes, Path.GetFileNameWithoutExtension(fileName), docTypeId, enrollmentId, Path.GetExtension(fileName).Replace(".", ""), comments: comments);
        }

当您一个一个上传每个文件时,它会被填充:

[HttpPost("upload")]
public async Task<IActionResult> Upload(IFormFile file)
{
    ...
}