为什么 Blueimp 的 jQuery-File-Upload 插件不触发回调?

发布于 2024-11-14 08:46:29 字数 1531 浏览 4 评论 0原文

我正在尝试 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);

但是我发现只有第一种方法有效,第二个没有做任何事情。

更奇怪的是,无论我如何绑定它们,似乎都没有其他回调(尤其是 progressstart)被触发:

$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 方法失败的原因是什么?为什么 progressstart 侦听器从未激活?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

翻了热茶 2024-11-21 08:46:29

我是 jQuery 文件上传插件的作者。

我没有解释为什么第三个示例代码中的 fileuploadadd 事件没有触发。
但是,如果您覆盖添加回调选项,则必须确保通过调用数据参数上的提交方法来启动文件上传,如 添加回调的选项文档(也记录在插件的源代码中)。

例如,以下代码应打印出不同的回调事件:

$('#fileupload').fileupload({
    add: function (e, data) {
        console.log('add');
        data.submit();
    },
    progress: function (e, data) {
        console.log('progress');
    },
    start: function (e) {
        console.log('start');
    }
}).bind('fileuploadadd', function (e, data) {
    console.log('fileuploadadd');
}).bind('fileuploadprogress', function (e, data) {
    console.log('fileuploadprogress');
}).bind('fileuploadstart', function (e) {
    console.log('fileuploadstart');
});

另请注意,该插件是模块化的,并且 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:

$('#fileupload').fileupload({
    add: function (e, data) {
        console.log('add');
        data.submit();
    },
    progress: function (e, data) {
        console.log('progress');
    },
    start: function (e) {
        console.log('start');
    }
}).bind('fileuploadadd', function (e, data) {
    console.log('fileuploadadd');
}).bind('fileuploadprogress', function (e, data) {
    console.log('fileuploadprogress');
}).bind('fileuploadstart', function (e) {
    console.log('fileuploadstart');
});

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.

戏舞 2024-11-21 08:46:29

这对我不起作用。

$('#fileupload').fileupload({url: '/url/to/server/'});

$('#fileupload').bind('fileuploaddone', function (e, data) {
   console.log('success!!!');

   console.log(e);

   console.log(data);

});

但这做到了!

$('#fileupload').fileupload({url: '/url/to/server/'}).bind('fileuploaddone', function (e, data) {
   console.log('success!!!');

   console.log(e);

   console.log(data);

});

我的第一个猜测是,在第一种情况下,您将事件绑定到实际的表单输入而不是 fileupload 对象,而在第二种情况下,通过使用链接,您实际上正在使用 fileupload 对象,我猜文档是不明确的,因为它读取:

$('#fileupload').bind('fileuploadadd', function (e, data) {/* ... */});

它应该读

$('#fileupload').fileupload().bind('fileuploadadd', function (e, data) {/* ... */});

This didn't work for me.

$('#fileupload').fileupload({url: '/url/to/server/'});

$('#fileupload').bind('fileuploaddone', function (e, data) {
   console.log('success!!!');

   console.log(e);

   console.log(data);

});

But this did!

$('#fileupload').fileupload({url: '/url/to/server/'}).bind('fileuploaddone', function (e, data) {
   console.log('success!!!');

   console.log(e);

   console.log(data);

});

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:

$('#fileupload').bind('fileuploadadd', function (e, data) {/* ... */});

And it should read

$('#fileupload').fileupload().bind('fileuploadadd', function (e, data) {/* ... */});
深陷 2024-11-21 08:46:29

如果定义了添加事件,则所有流程回调都不会触发。

$(function(){
    var fileupload=$('#fileupload').fileupload({
        url: 'fileupload.php',
        dataType: 'json',
        add: function(e, data) {
            data.submit();
        },
    })
    .on('fileuploadprocessalways', function (e, data) {
        //Won't be triggered if add callback is defined
    })
});

If the add event is defined, all the process callbacks will not fire.

$(function(){
    var fileupload=$('#fileupload').fileupload({
        url: 'fileupload.php',
        dataType: 'json',
        add: function(e, data) {
            data.submit();
        },
    })
    .on('fileuploadprocessalways', function (e, data) {
        //Won't be triggered if add callback is defined
    })
});
一绘本一梦想 2024-11-21 08:46:29

相反,

$('#fileupload').bind('fileuploadadd', function (e, data) {/*...*/});

我使用了它,

$('#fileupload').bind('fileuploadchange', function (e, data) {/*...*/});

并且它对我有用。

Instead of,

$('#fileupload').bind('fileuploadadd', function (e, data) {/*...*/});

I used,

$('#fileupload').bind('fileuploadchange', function (e, data) {/*...*/});

and it worked for me.

对你而言 2024-11-21 08:46:29

不确定这是否解决了您的问题,但对我来说,以下内容不起作用(应该根据文档工作:

$uploadButton.bind 'fileuploadchange', (e, data) =>
  # Do something

但是,以下内容有效:

$uploadButton.bind 'change', (e, data) =>
  # Do something

Not sure if this solves your problem or not, but for me, the following does not work (should work per the documentation:

$uploadButton.bind 'fileuploadchange', (e, data) =>
  # Do something

However, the following works:

$uploadButton.bind 'change', (e, data) =>
  # Do something
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文