提供的“HttpContent”实例无效。它没有“多部分”内容类型的标题和“边界”参数 [英] Invalid 'HttpContent' instance provided. It does not have a 'multipart' content-type header with a 'boundary' parameter

查看:2030
本文介绍了提供的“HttpContent”实例无效。它没有“多部分”内容类型的标题和“边界”参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公共异步任务< p>列表与LT;串GT;>如果(Request.Content.IsMimeMultipartContent(form-data))
{
string uploadPath = HttpContext.Current.Server.MapPath ( 〜/上传);

var streamProvider = new MyStreamProvider(uploadPath);

await Request.Content.ReadAsMultipartAsync(streamProvider);

返回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!);
抛出新的HttpResponseException(response);




$ b $ p
$ b $ p然后我发布一个请求到邮差上面的动作。
我把content-type头设置为multipart / form-data
,但是在执行动作的过程中发生错误。
这里是错误信息正文:
$ b

提供了无效的'HttpContent'实例,它没有'multipart'内容类型头'boundary'参数。\r\\\
Parameter name:content



我去邮递员标题,但我发现请求标题content-type in被设置为application-json。





任何人都可以帮我吗?

这是你的好。



你真正的问题是与邮递员的请求,所以只要删除'Content-Type:multipart / form-数据'从请求头输入。
将文件上传为表单数据并发送请求就足够了。



查看当设置Content-手动输入,而不输入:



由于您只设置内容类型,所以邮差知道要设置内容类型和边界

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);
        }
    }

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 :

"Invalid 'HttpContent' instance provided. It does not have a 'multipart' content-type header with a 'boundary' parameter.\r\nParameter name: content"

i went to the postman headers but i found that request header content-type in was set to application-json.

can anyone help me?

解决方案

You are looking on the response header which is json format and this is ok for you.

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.

Look what happen when you set the Content-Type manually vs. when you not:

Postman knows to set both the content type and boundary, since you set only the content type

这篇关于提供的“HttpContent”实例无效。它没有“多部分”内容类型的标题和“边界”参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆