简体   繁体   English

Polymer:使用application / json contentType发布带有文件上传的铁表格

[英]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. 我们正在尝试创建一个基于聚合物的应用程序,其中用户需要在一个部分中将文件上传到处理该应用程序所有数据的Web服务。

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. 我们发现的所有示例都使用FormData对象并重置iron-form contentType字段,以使iron-form使用多部分编码。 The webservice we use only accepts application/json so that's not really an option for us. 我们使用的Web服务仅接受application/json因此对我们而言并不是一个选择。

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. 理想情况下, iron-form将仅支持<paper-input type="file">并将文件内容加载到JSON字段中,但仅提交文件名。 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? 也许我们需要实现pre-submit事件处理程序,以使用JavaScript读取文件并将相关的iron-form字段设置为文件的文本?

The behaviour is as specified in HTML5 : 该行为是在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. 否则,如果field元素是其type属性处于File Upload状态的输入元素,则对于在输入元素中选择的每个文件,将一个条目添加到表单数据集中,并将其名称命名为file(由...组成)名称,类型和主体)作为值,然后将type作为类型。

And in 4.10.22.8 (content-type text/plain ): 在4.10.22.8中(内容类型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. 请注意, application/json基本上回落到了那个位置。

So you will have to read the file content in JavaScript and put it into the JSON-payload yourself. 因此,您将必须使用JavaScript读取文件内容并将其放入JSON有效负载中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM