我正在编写一个 web api,它有一个 post 方法接受从 UI 上传的文件。
public async Task<List<string>> PostAsync()
{
if (Request.Content.IsMimeMultipartContent("form-data"))
{
string uploadPath = HttpContext.Current.Server.MapPath("~/uploads");
var streamProvider = new MyStreamProvider(uploadPath);
await Request.Content.ReadAsMultipartAsync(streamProvider);
return streamProvider.FileData
.Select(file => new FileInfo(file.LocalFileName))
.Select(fi => "File uploaded as " + fi.FullName + " (" + fi.Length + " bytes)")
.ToList();
}
else
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid Request!");
throw new HttpResponseException(response);
}
}
然后我通过邮递员发布对上述操作的请求。我将 content-type 标头设置为 multipart/form-data 但在执行操作期间发生错误。这是错误消息正文:
"提供的 'HttpContent' 实例无效。它没有带有 'boundary' 参数的 'multipart' 内容类型标头。\r\n参数名称: 内容"
我去了邮递员标头,但我发现请求标头内容类型设置为 application-json。
谁能帮我?