【发布时间】:2019-11-01 21:01:24
【问题描述】:
我正在尝试使用 Flurl 使用 AddFile 上传文件。
生成的IFormFileCollection 为空,尽管在查看具有正确内容长度的Request.Form.Files[0] 时我能够看到该项目。
创建请求:
public Task<HttpResponseMessage> UploadImage(string fileName, MemoryStream stream)
{
stream.Position = 0;
_baseUrl
.AppendPathSegments("uploadImage")
.PostMultipartAsync(mp => mp
.AddFile("files", stream, fileName))
}
处理请求:
[HttpPost]
[Route("uploadImage")]
public async Task<HttpResponseMessage> UploadImages([FromForm] IFormFileCollection files)
{
//files is null, but Request.Form.Files[0] in the immediate window shows the file.
}
一个常见问题似乎是参数名称与 Content-Disposition 标头中的名称不匹配,但我将它们都更新为 files,但我仍然遇到同样的问题。
【问题讨论】:
-
我不会回答,因为我不知道,但这里有几件事可以尝试:1) 删除
[FromForm]属性,2) 因为在此示例中您只上传 1 个文件, 使用IFormFile而不是IFormFileCollection。
标签: c# asp.net-core multipartform-data flurl iformfile