7

I use file upload with webapi in my project. I am testing with Postman. However, Request.Content.IsMimeMultipartContent() always returns false.

Postman screenshot:

enter image description here

enter image description here

FileUploadController Code:

public async Task<HttpResponseMessage> UserImageUpload()
    {
        try
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var userImageUploadPath = HttpContext.Current.Server.MapPath(CommonParameters.UserProfileImageServerPath);
            var streamProvider = new CustomMultipartFormDataStreamProvider(userImageUploadPath);
            await Request.Content.ReadAsMultipartAsync(streamProvider);

            var files = new List<string>();
            foreach (MultipartFileData file in streamProvider.FileData)
            {
                files.Add(Path.GetFileName(file.LocalFileName));
            }

            return Request.CreateResponse(HttpStatusCode.OK, files);
        }
        catch (Exception exception)
        {
            logger.ErrorFormat("An error occured in UserImageUpload() Method - Class:FileUploadController - Message:{0}", exception);
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }
    }
live-love
  • 48,840
  • 22
  • 240
  • 204
Batuhan Avlayan
  • 411
  • 1
  • 4
  • 19

5 Answers5

10

This is Postman bug. Try removing the Content-Type header. When sending the actual Post, the browser will automatically add the proper header and create the boundary.

Tiago Gouvêa
  • 15,036
  • 4
  • 75
  • 81
2

There is no need to mention Content-Type in headers in Postman, I tried sending attachments without Content-Type it works fine for me. When i used Content-Type: multipart/formdata it throws an error saying "Could not get any response". Postman sends your file attachments also with Content-Type →text/plain; charset=utf-8.

Akshay
  • 407
  • 1
  • 4
  • 14
2

There are few things to consider:

1) Don't send any Content Type Header if you are using Postman

2) Specify the Key for your choosen file in Body (PFB Screenshot for your reference)

Please check below postman screenshot for your reference.

Kumar Lachhani
  • 218
  • 1
  • 17
1

You need to uncheck Content-Type if you have it checked, and let Postman add it for you, like in this picture:

enter image description here

enter image description here

live-love
  • 48,840
  • 22
  • 240
  • 204
0

Might by a bit late. I encountered the same error in ARC and resolved by providing a name for the file field (after the blue check mark on your second screenshot)

Migg
  • 464
  • 5
  • 13