提供的“HttpContent"实例无效.它没有带有“边界"参数的“多部分"内容类型标头 [英] Invalid 'HttpContent' instance provided. It does not have a 'multipart' content-type header with a 'boundary' parameter
问题描述
我正在编写一个 web api,它有一个 post 方法接受从 UI 上传的文件.
I'm writing a web api that have a post method accepting files from uploaded from 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);
}
}
然后我通过邮递员发布对上述操作的请求.我将内容类型标头设置为 multipart/form-data但是在执行动作的过程中发生了错误.这是错误消息正文:
then i post a request to above action by postman. I set content-type header to multipart/form-data but an error occures during execution of action. here is error message body :
提供的 'HttpContent' 实例无效.它没有带有 'boundary' 参数的 'multipart' 内容类型标头. 参数名称:内容"
"Invalid 'HttpContent' instance provided. It does not have a 'multipart' content-type header with a 'boundary' parameter. Parameter name: content"
我去了邮递员标头,但我发现请求标头内容类型设置为 application-json.
i went to the postman headers but i found that request header content-type in was set to application-json.
谁能帮帮我?
推荐答案
您正在查看响应头,它是 json 格式,这对您来说没问题.
You are looking on the response header which is json format and this is ok for you.
您真正的问题在于邮递员请求,因此只需从请求标头中删除Content-Type: multipart/form-data"条目.将文件作为表单数据上传并发送请求就足够了.
Your real problem is with the postman request, so just remove the 'Content-Type: multipart/form-data' entry from request header. It's enough to upload a file as form-data and send the request.
看看当您手动设置 Content-Type 与不设置时会发生什么:
Look what happen when you set the Content-Type manually vs. when you not:
Postman 知道同时设置内容类型和边界,因为您只设置了内容类型
这篇关于提供的“HttpContent"实例无效.它没有带有“边界"参数的“多部分"内容类型标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!