Disper
Disper

Reputation: 745

Upload a file as json

I would like to submit form data in a json format as it is shown on example #9 http://darobin.github.io/formic/specs/json/.

My form looks like this:

<form id='myform'
      enctype='application/json'
      action=`http://localhost:8080`
      method='post'>
    <input type='text' name='textInput'>
    <input id='file' type='file' name='file' multiple>
    <button>submit</button>
</form>

But unfortunately when I try to submit a form I see that they're sent as following formData (it's not JSON, and only file names are sent, without encoded files data):

textInput:asdasd
file:Screenshot from 2014-10-14 18:10:05.png
file:Screenshot from 2014-10-25 11:25:26.png

I've also tried to stringify my form with JSON.stringify($("#myform").serializeArray()); but the result: "[{"name":"textInput","value":"asdasdasd"}]" is missing data from file inputs.

What is the best way to send form data as json?

Upvotes: 0

Views: 57

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075059

As the first working draft of that was May 2014, I very much doubt it's got any significant browser support yet.

For the moment, as far as I know the only two ways to send JSON are:

  1. As one field in a standard URI-encoded form; on the server, you'd get the value of the field and then read the JSON from it. Your file fields would be sent in the usual way.

  2. Via ajax. But to send files that way, you have to have a modern browser (you can't use IE8 or 9).

For now, option #1 is probably your best option.

Upvotes: 1

Related Questions