Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 return 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);
        }
    }
like image 279
Batuhan Avlayan Avatar asked Feb 19 '16 09:02

Batuhan Avlayan


2 Answers

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.

like image 56
Tiago Gouvêa Avatar answered Sep 28 '22 06:09

Tiago Gouvêa


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.

like image 36
Akshay Avatar answered Sep 28 '22 07:09

Akshay