0

i am passing data from the client to the server (WebAPI or MVC) as a pure json as the http post body while using Content-Type:application/json.

it works great but now i need to add to the same post request a file which the user may need to add to that http post.

As per my understanding i can not use formData object because i can not mix the body with json and the formData.

i thought about using FileReader javascript function and pass the encoded string as one of the json properties but it smells as a "bad practice".

is there a better way to do so?

Omtechguy
  • 3,321
  • 7
  • 37
  • 71
  • Have you thought about using two Post requests: one for the JSON data and another for the File content? – sketchedin Feb 05 '18 at 14:09
  • @sketchedin Sure, but i am looking for a one post request solution. – Omtechguy Feb 05 '18 at 14:10
  • 3
    Here is a good discussion on some options: https://stackoverflow.com/questions/4083702/posting-a-file-and-associated-data-to-a-restful-webservice-preferably-as-json – sketchedin Feb 05 '18 at 14:17
  • I would just have encoded the json object and appended it to a FormData along with the file, easy to parse the json on the server – Endless Feb 06 '18 at 18:30

0 Answers0