2

How can i upload file and model parameters in mvc WEB API 2.

I have following code, which works just fine, if i remove model from the action, but with the model, I am receiving following error.

"message": "The request entity's media type 'multipart/form-data' is not supported for this resource.", "exception_message": "No MediaTypeFormatter is available to read an object of type 'CreateTicketDTO' from content with media type 'multipart/form-data'.",

 [HttpPost] [Route("api/support/tickets/")] public async Task<HttpResponseMessage> Insert(CreateTicketDTO dto) { if(dto == null) return Request.CreateResponse(HttpStatusCode.BadRequest, "Please supply required parameters"); var provider = new MultipartMemoryStreamProvider(); var a = await Request.Content.ReadAsMultipartAsync(provider); foreach (var file in provider.Contents) { var filename = file.Headers.ContentDisposition.FileName.Trim('\"'); var buffer = await file.ReadAsByteArrayAsync(); //Do whatever you want with filename and its binaray data. } using (_ticketService) { var ticket = await _ticketService.CreateNewTicket(dto); return Request.CreateResponse(HttpStatusCode.OK, ticket); } } 

I am creating a post request in Postman.

8
  • change media type from multipart/form-data to application/x-www-form-urlencoded Commented Jun 23, 2016 at 14:10
  • that produces following error: Invalid 'HttpContent' instance provided. It does not have a content type header starting with 'multipart/'.\r\nParameter name: content", Commented Jun 23, 2016 at 14:12
  • stackoverflow.com/questions/28369529/… Commented Jun 23, 2016 at 14:15
  • @venky, there is no example with model+file in this thread. Commented Jun 23, 2016 at 14:29
  • string root = HttpContext.Current.Server.MapPath("~/App_Data"); var provider = new MultipartMemoryStreamProvider(root); may it work. Commented Jun 23, 2016 at 14:36

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.