HttpRequest.Files is empty when sending through PO

2019-06-02 10:37发布

I'm following this blog post to upload an image using C# Web API.

The article explains how to do it using ARC and it works fine.

But when I'm trying to do the same using POSTMAN it's failing.

Here is my request snapshot.

enter image description here

enter image description here

2条回答
Ridiculous、
2楼-- · 2019-06-02 11:07

Humph! This was hell tricky. You are doing everything correct except that you are setting the Content-Type header explicitly in the tool. You must not do that. Whenever you attach the files in form-data in the Body tab in the tool, Postman auto-detects the Content-Type and sends it in your post request.

Setting up Content-Type to "multipart/form-data" involves a complex concept of setting up boundaries of multiple parts as detailed here. So setting up Content-Type header explicitly messes up the request. Heavy lifting of setting up the boundaries is done automatically for you by postman tool which is why it doesn't want you to set the content-type explicitly in this case. Please see how I've set only Authorization header while uploading the image file on my system:

enter image description here

You might not even need this Authorization header if there isn't any authentication on your web server. So effectively Headers tab in your case should simply be empty i.e. no key value pairs at all.

Note: Just for information, the correct content type for image files is multipart/form-data even though you don't need to set it explicitly in the tool.

查看更多
一夜七次
3楼-- · 2019-06-02 11:12

In the post you referrer to the data is being uploaded as "x-www-form-url-encoded"

Your Postman screen shot shows you are uploading it as "form-data"

Additionally, you adding a key "image01" where the ARC example doesn't appear to be sending a key.

If you want to upload the file using form-data you need a different approach:

// POST api/files
public async Task<HttpResponseMessage> Post()
{
    // Check if the request contains multipart/form-data.
    if (!Request.Content.IsMimeMultipartContent())
    {
        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
    }

    string root = HttpContext.Current.Server.MapPath("~/App_Data");
    var provider = new MultipartFormDataStreamProvider(root);

    string value;

    try
    {
        // Read the form data and return an async data.
        var result = await Request.Content.ReadAsMultipartAsync(provider);

        // This illustrates how to get the form data.
        foreach (var key in provider.FormData.AllKeys)
        {
            foreach (var val in provider.FormData.GetValues(key))
            {
                // return multiple value from FormData
                if (key == "value")
                    value = val;
            }
        }                       

        if (result.FileData.Any())
        {                    
            // This illustrates how to get the file names for uploaded files.
            foreach (var file in result.FileData)
            {
                FileInfo fileInfo = new FileInfo(file.LocalFileName);
                if (fileInfo.Exists)
                {
                   //do somthing with file
                }
            }
        }


        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, value);
        response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = files.Id }));
        return response;
    }
    catch (System.Exception e)
    {
        return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
    }
}
查看更多
登录 后发表回答