骨干。表单带有文件上传,如何处理?
我只想通过 REST API 组织工作流程。我有一个允许上传图像的表单(enctype="multipart/form-data")。我如何通过主干处理此表单?请帮助我,如何使用文件字段将其序列化为 JSON。
谢谢。 维塔利
I want to organize the workflow only through the REST API. I have a form that allows to upload image (enctype="multipart/form-data"). How do I handle this form via backbone? Help me please, how I can to serialize it into JSON with a file field.
Thanks.
Vitaliy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您使用的是 HTML5,则可以使用文件 api 中的 readAsDataURL 方法来读取它并将其存储在模型上。
这是我用来读取和存储的代码。
If you are using HTML5, you can use the readAsDataURL method from the file api to read and store it on your models.
Here's the code i use to read and store.
您可以尝试 jquery.iframe.transport 插件。
You could try the jquery.iframe.transport plugin.
恕我直言,您无法将文件序列化为 JSON。
如果您需要随文件一起发送一些数据,您可以使用 POST 方法将它们作为查询参数发送。
IMHO, you cannot serialize a file into JSON.
If you need to send some data along with the file you can send them as query params with POST method.
没有通过 AJAX 提交文件的好方法。所以我写了一个函数来伪造它——它将一个秘密 iframe 插入到你的 DOM 中,该 iframe 永远不可见,但仍然可以作为提交表单的目标,并且它会安装一个函数供你的响应调用,当文件已上传。
让您的上传表单的提交按钮触发我编写的这个函数。它使用 jQuery,因为它简单又漂亮,但原则上这不是绝对必要的:
然后让您的表单处理程序执行文件解析并保存返回字符串:
您的表单可以如下所示:
(#documents 是这个 div form 存在于其中。可能是任何 DOM 元素,它只需要一个家。)
There's no good way to submit a file via AJAX. So I wrote a function to fake it--it inserts a secret iframe into your DOM that is never visible but still works as a target to submit your form on, and it installs a function for your response to call that cleans house when the file is uploaded.
Have your upload form's submit button fire this function I wrote. It uses jQuery because it's easy and nice, but in principle that shouldn't be strictly necessary:
Then have your form handler that does your file parsing and saving return the string:
Your form can look like:
(#documents is the div that this form lives in. Could probably be any DOM element, it just needs a home.)
根据安东尼的回答(https://stackoverflow.com/a/10916733/2750451),我编写了一个解决方案在基于 defer 对象的 CoffeeScript 中。
然后,您可以这样使用它
在服务器端处理它(因为它是Base64编码的),这里是RoR中的解决方案(基于 https://stackoverflow.com/a/16310953/2750451)
Based on Anthony answer (https://stackoverflow.com/a/10916733/2750451), I've written a solution in coffeescript based on a defer object.
Then, you could use it this way
To handle it on the server side (because it's Base64 encoded), here the solution in RoR (based on https://stackoverflow.com/a/16310953/2750451)
详细阐述蔡美儿的回答。您需要将图像处理添加到 Backbone.Form.editors 中,例如
您可以使用上面的代码,如下所示
渲染表单:
To ellaborate on Anthony Chua's answer. You need to add Image handling to
Backbone.Form.editors
likeYou can use above code as follows
Render form using:
HTML5(包括 IE9)之前无法通过 AJAX 提交文件。
您需要通过 ajax 同步模型属性,然后使用该文件发送另一个 html 表单帖子,然后以某种方式同步它们。通常,通过ajax保存模型,获取id,将id添加到其他表单,然后发布文件。
“jquery.form”中的 jQuery 插件可以帮助您构建一个表单来发布文件。它管理“隐藏的 iframe 技巧”,因此对于最终用户来说它看起来就像 AJAX。
您可能只需要花一些时间谷歌搜索“隐藏的 iframe 技巧”......
It is not possible to submit a file over AJAX before HTML5 (including IE9).
You need to sync the model attributes over ajax, and then send another html form post with the file, and then sync them up somehow. Generally, save the model over ajax, get an id back, add the id to the other form, and then post the file.
The jQuery plug in "jquery.form" may help you to construct a form to post the file. It manages the "hidden iframe trick" so that it looks like AJAX to the end user.
You might just need to spend some time googling "hidden iframe trick" ...