How to solve file upload error in postman?

I am using file upload from webapi in my project. I am testing a 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); } } 
+5
source share
3 answers

This is a postman mistake. Try removing the Content-Type header. When sending the actual message, the browser will automatically add the correct header and create a border.

+8
source

There is no need to mention Content-Type in the headers in Postman, I tried to send attachments without Content-Type, it works fine for me. When I used Content-Type: multipart/formdata , it gives the error message "Could not get response." The postman also sends your attachments using Content-Type →text/plain; charset=utf-8 Content-Type →text/plain; charset=utf-8 .

+1
source

Maybe a little late. I ran into the same error in ARC and decided by providing a name for the file field (after the blue checkmark in the second screenshot)

0
source

Source: https://habr.com/ru/post/1243376/


All Articles