[英]Jquery file upload in rails on button click
I am using jquery-fileupload plugin ( https://github.com/tors/jquery-fileupload-rails ) for showing file upload progress in my rails app. 我正在使用jquery-fileupload插件( https://github.com/tors/jquery-fileupload-rails )在Rails应用程序中显示文件上传进度。 The following code below works fine.
下面的代码可以正常工作。
In my index.html.erb i have 在我的index.html.erb中,我有
<script id="template-upload" type="text/x-tmpl">
<div class="upload">
{%=o.name%}
<div class="progress"><div class="bar" style="width: 0%;"></div></div>
</div>
</script>
In my projects js.coffee file i have 在我的项目js.coffee文件中
$('#import_form').fileupload
dataType: "json"
add: (e, data) ->
data.context = $(tmpl("template-upload", data.files[0]))
$('#import_form').append(data.context)
data.submit()
progress: (e, data) ->
if data.context
progress = parseInt(data.loaded / data.total * 100, 10)
data.context.find('.bar').css('width', progress + '%')
But i want the upload to start when i click a button with id import_button instead of automatically. 但是我希望当我单击ID为import_button的按钮而不是自动单击时开始上载。 How can i achieve that ?
我该如何实现?
I tried doing the following, but i get the error message "data is undefined" 我尝试执行以下操作,但收到错误消息“数据未定义”
$('#import_form').fileupload
dataType: "json"
add: (e, data) ->
data.context = $(tmpl("template-upload", data.files[0]))
$('#import_form').append(data.context)
$("#import_button").off("click").on "click", ->
data.submit()
progress: (e, data) ->
if data.context
progress = parseInt(data.loaded / data.total * 100, 10)
data.context.find('.bar').css('width', progress + '%')
Please Help Thank You 请帮忙谢谢
One special callback is the add
callback, as it provides a submit method for the data argument, that will start the file upload: 一个特殊的回调是
add
回调,因为它为data参数提供了一个commit方法,它将开始文件上传:
$('#fileupload').fileupload({
add: function (e, data) {
data.submit();
}
});
It's from jQuery Plugin Wiki 来自jQuery Plugin Wiki
So if you don't want to start the upload automatically use add
callback, but don't call submit method. 因此,如果您不想自动开始上传,请使用
add
回调,但不要调用Submit方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.