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.
string root = HttpContext.Current.Server.MapPath("~/App_Data");var provider = new MultipartMemoryStreamProvider(root);may it work.