Batuhan Avlayan
Batuhan Avlayan

Reputation: 421

How to solve file upload error in Postman?

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

Upvotes: 7

Views: 8002

Answers (5)

Kumar Lachhani
Kumar Lachhani

Reputation: 218

There are few things to consider:

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

  2. Specify the form-data Key for your choosen file in Body (form-data) (PFB Screenshot for your reference)

Please check below postman screenshot for your reference.

Upvotes: 2

live-love
live-love

Reputation: 52534

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

Upvotes: 1

Migg
Migg

Reputation: 484

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)

Upvotes: 0

Akshay
Akshay

Reputation: 457

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.

Upvotes: 2

Tiago Gouv&#234;a
Tiago Gouv&#234;a

Reputation: 16820

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.

Upvotes: 10

Related Questions