Guss
Guss

Reputation: 32315

Polymer: post iron-form with file upload using application/json contentType

We're trying to create a polymer based application, where in one part the user needs to upload a file to the web service that's handling all the data for the application.

All the examples we've found use a FormData object and reset the iron-form contentType field to cause iron-form to use the multi-part encoding. The webservice we use only accepts application/json so that's not really an option for us.

Ideally iron-form would just support a <paper-input type="file"> and would load the file contents into the JSON field, but instead it just submits the file name. Maybe we need to implement the pre-submit event handler to read the file in JavaScript and set the relevant iron-form field to the text of the file?

Upvotes: 0

Views: 370

Answers (1)

geekonaut
geekonaut

Reputation: 5954

The behaviour is as specified in HTML5:

Otherwise, if the field element is an input element whose type attribute is in the File Upload state, then for each file selected in the input element, append an entry to the form data set with the name as the name, the file (consisting of the name, the type, and the body) as the value, and type as the type.

And in 4.10.22.8 (content-type text/plain):

If the entry's type is "file", replace its value with the file's name only.

Note that application/json is basically falling back to that.

So you will have to read the file content in JavaScript and put it into the JSON-payload yourself.

Upvotes: 1

Related Questions