2

I added this line to my WebApiConfig

config.Formatters.XmlFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("multipart/form-data"));

(From my research I found that this is the only way to receive data and file at the same time, PS: Can I send JSON data and file data at the same time some way?) because I couldn't receive multipart/form-data. Now I can receive the binary files only (from HttpPostedFile httpPostedFile = HttpContext.Current.Request.Files[0];)... However I can't receive the actual data from my bind model in my action public IHttpActionResult FooAction([FromBody]FooBindModel fooBindModel) (the bind model is null). It seems that it doesn't bind the data or something? (the data are primitive strings and integers so I don't think that the type is the problem). PS: I am using to send my data Postman form-data.

john
  • 31
  • 1
  • 1
  • 3
  • _Can I send JSON data and file data at the same time some way?_ Yes, you encode your file to a base64 string then include it with the rest of your json data. Your WebAPI app will not need to handle "multipart/form-data". – Jasen Jan 10 '18 at 18:34
  • @Jasen Is the encoding of the file to base64 string is done by the HTTP Request composer ? Can I use postman to do this? Can I see some samples/tutorials/advice? – john Jan 10 '18 at 19:11
  • You'd do this with code. Client-side with [JavaScript](https://stackoverflow.com/questions/36280818/how-to-convert-file-to-base64-in-javascript) then reverse the process in [C#](https://stackoverflow.com/questions/25919387/c-sharp-converting-file-into-base64string-and-back-again). Your Postman request would be "application/json". – Jasen Jan 10 '18 at 19:16
  • @Jasen Can postman encode it itself? without code – john Jan 10 '18 at 20:16
  • Write a short stub to do it for you (you could do it in a jsfiddle or a Visual Studio test method). Then save the string so you can reuse it. – Jasen Jan 10 '18 at 20:19

0 Answers0