如何在Rails 4(使用jquery)中上传多个文件?
I added an upload form so people can upload HTML files to my site. How do I allow multiple uploads at once? I added the jquery-fileupload-rails gem which lets the user select multiple files. However, only one file gets processed by the server. How do I fix this?
我添加了一个上传表单,这样人们就可以将HTML文件上传到我的站点。如何同时允许多个上传?我添加了jquery-fileupload-rails gem,它允许用户选择多个文件。但是,服务器只处理一个文件。我怎么修复这个?
More Details
更多的细节
My code is below, based on this Railscast. Note I'm not dealing with a model in the form, so I use field_tags instead. I didn't add name to file_field_tag
, since it's name didn't change after adding files, though that may be part of the problem.
我的代码在下面,基于这个Railscast。注意,我不是在处理表单中的模型,所以我使用field_tags代替。我没有向file_field_tag添加名称,因为它的名称在添加文件后没有更改,尽管这可能是问题的一部分。
<%= form_tag import_items_path, multipart: true, id: "upload" do %>
<%= file_field_tag :file, multiple: true %>
<%= submit_tag "Import" %>
<% end %>
And here's some the JS for the fileupload jQuery:
下面是fileupload jQuery的一些JS:
jQuery ->
$('#upload').fileupload
When I add 2 files and click upload, only 1 file gets processed. Here's the log:
当我添加两个文件并单击upload时,只有一个文件被处理。日志:
Parameters: {"utf8"=>"✓", "file"=> #< ActionDispatch::Http::UploadedFile:0x... @tempfile=#< Tempfile:/var/folders/gl/...>, @original_filename="sample.html", @content_type="text/html",... name=\"file\"; filename=\"sample.html\"...}
参数:{“use utf8”= >“✓”、“文件”= > # < Http ActionDispatch::::UploadedFile:0 x……@tempfile = # < Tempfile:/ var /文件夹/ gl /…>,@original_filename = "样本。html”,@content_type = " text / html ",……name = \ \“文件”;sample . html文件名= \“\”…}
I tried ajaxifying the form, but that didn't help either. How can I get jquery-fileupload-rails in Rails 4 to work?
我试着对表单进行ajax化,但这也无济于事。如何在Rails 4中获得jquery- fileuploadrails ?
1 个解决方案
#1
3
If you change <%= file_field_tag :file, multiple: true %>
to <%= file_field_tag 'files[]', multiple: true %>
it should work.
如果您将<%= file_field_tag:file, multiple: true %>改为<%= file_field_tag 'files[], multiple: true %>,应该可以工作。
See this related SO post.
见此相关帖。
更多相关文章
- Asp。NET MVC Ajax-Post表单和Ajax-Get
- 使用jquery post发布PHP文件
- 解析jquery实现回车键提交表单
- JQUERY中做表单验证,谁有带时间的日期选择控件?
- 深入学习jQuery选择器系列第七篇——表单选择器
- jQuery Validate 表单验证插件
- 如何将表单转换成json通过AJAX传递?
- 从JQuery文件输入中获取数据
- jQuery LightBox(SlimBox):如何在其中填充ASPX文件?