为什么 Blueimp 的 jQuery-File-Upload 插件不触发回调?
我正在尝试 Blueimp 的 jQuery-File-Upload 插件,根据 演示 看起来非常有前途。
它非常容易实现:
var $uploadButton = $("#fileop-upload");// <input type="file" id="fileop-upload" [etc] />
$uploadButton.fileupload({
url : "//domain/path/to/receive-uploaded-files"
});
所选文件可以正常上传,无需按预期刷新页面,但当然,使用这样的最小配置,用户不会收到任何通知。这就是插件的回调派上用场的地方。
根据文档,有两种定义回调的方法。例如,可以将 add
事件(每当选择要上传的文件时触发)添加到原始配置对象中,如下所示:
$uploadButton.fileupload({
add : addFileListener,
url : "//domain/path/to/receive-uploaded-files"
});
或者:
$uploadButton.bind("fileuploadadd", addFileListener);
但是我发现只有第一种方法有效,第二个没有做任何事情。
更奇怪的是,无论我如何绑定它们,似乎都没有其他回调(尤其是 progress
和 start
)被触发:
$uploadButton.fileupload({
add : addFileListener,
progress : progressListener,
start : startListener,
url : "//domain/path/to/receive-uploaded-files"
});
或者
$uploadButton.fileupload({
add : addFileListener,
url : "//domain/path/to/receive-uploaded-files"
});
$uploadButton.bind("fileuploadprogress", progressListener");
$uploadButton.bind("fileuploadstart", startListener");
我定义了引用的侦听器函数,并且代码不会报告任何错误或警告。
.bind
方法失败的原因是什么?为什么 progress
或 start
侦听器从未激活?
I'm experimenting with Blueimp's jQuery-File-Upload plugin, which judging by the demo looks very promising.
It's really easy to implement:
var $uploadButton = $("#fileop-upload");// <input type="file" id="fileop-upload" [etc] />
$uploadButton.fileupload({
url : "//domain/path/to/receive-uploaded-files"
});
The selected files are uploaded fine without refreshing the page as expected, but of course with a minimal configuration like this the user won't get any notification. Here's where the plugin's callbacks would come in handy.
According to the documentation there are two ways to define callbacks. For example the add
event (which fires whenever a file is selected for uploading) can be added in the original configuration object like this:
$uploadButton.fileupload({
add : addFileListener,
url : "//domain/path/to/receive-uploaded-files"
});
or alternatively:
$uploadButton.bind("fileuploadadd", addFileListener);
However I've found that only the first approach works, the second one doesn't do anything.
It is even more curious that no other callbacks -- especially progress
and start
-- seems to be firing not matter how I bind them:
$uploadButton.fileupload({
add : addFileListener,
progress : progressListener,
start : startListener,
url : "//domain/path/to/receive-uploaded-files"
});
or
$uploadButton.fileupload({
add : addFileListener,
url : "//domain/path/to/receive-uploaded-files"
});
$uploadButton.bind("fileuploadprogress", progressListener");
$uploadButton.bind("fileuploadstart", startListener");
I have the referred listener functions defined, and the code doesn't report any errors or warnings.
What is the explanation for the .bind
method's failure, and why doesn't the progress
or the start
listeners ever activate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我是 jQuery 文件上传插件的作者。
我没有解释为什么第三个示例代码中的 fileuploadadd 事件没有触发。
但是,如果您覆盖添加回调选项,则必须确保通过调用数据参数上的提交方法来启动文件上传,如 添加回调的选项文档(也记录在插件的源代码中)。
例如,以下代码应打印出不同的回调事件:
另请注意,该插件是模块化的,并且 UI 版本(在下载示例中使用)使用了回调选项,该选项将被上述代码覆盖。
这就是事件绑定如此有用的原因,因为它允许定义额外的回调方法,而无需覆盖通过选项对象设置的回调。
I'm the author of the jQuery File Upload plugin.
I don't have an explanation why the fileuploadadd event in your third example code doesn't fire.
However, if you override the add callback option, you have to make sure the file upload is started by calling the submit method on the data argument, as explained in the Options documentation for the add callback (and also documented in the source code of the plugin).
e.g. the following code should print out the different callback events:
Note also that the plugin is modular and the UI version (used in the download example) makes use of the callback options which would be overridden with the above code.
That's why the event binding is so useful, as it allows to define additional callback methods without overriding the callbacks set via the options object.
这对我不起作用。
但这做到了!
我的第一个猜测是,在第一种情况下,您将事件绑定到实际的表单输入而不是 fileupload 对象,而在第二种情况下,通过使用链接,您实际上正在使用 fileupload 对象,我猜文档是不明确的,因为它读取:
它应该读
This didn't work for me.
But this did!
My first guess is that in the first case you are binding the event to the actual form input instead of the fileupload object, and in the second, by using chainning you are actually using the fileupload object, I guess the documentation is ambiguous since it reads:
And it should read
如果定义了添加事件,则所有流程回调都不会触发。
If the add event is defined, all the process callbacks will not fire.
相反,
我使用了它,
并且它对我有用。
Instead of,
I used,
and it worked for me.
不确定这是否解决了您的问题,但对我来说,以下内容不起作用(应该根据文档工作:
但是,以下内容有效:
Not sure if this solves your problem or not, but for me, the following does not work (should work per the documentation:
However, the following works: