blueimp jquery-file-upload不会在不成功的添加上抛出错误 [英] blueimp jquery-file-upload Doesn't throw error on unsuccessful add

查看:299
本文介绍了blueimp jquery-file-upload不会在不成功的添加上抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个插件中,我们可以设置文件上传的各种条件/限制(例如acceptFileTypes,maxFileSize,maxNumberOfFiles等)。

In this plugin we can set various conditions/restrictions on file upload (e.g. acceptFileTypes, maxFileSize, maxNumberOfFiles, etc..)

每当添加文件时,事件被触发 fileuploadadd ,一旦文件成功添加,另一个事件将被触发 fileuploadadded

Whenever a file is added, an event is fired "fileuploadadd" and once the file is added successfully another event is fired "fileuploadadded".

但是,当添加文件并且它失败了这些条件/限制之一时(例如,用户尝试上传2GB文件或文本文件而不是图像),则无法以编程方式找出这种情况。

However, when a file is added and it fails one of these condition/restriction (e.g. user tries to upload 2GB file, or text file instead of an image), there is no way find out programatically that this has occurred.

当此条件失败时,不会触发任何事件。我可以用什么来检查用户选择的文件是否有错误,因此没有添加到队列中。

When this conditions fail, no event is fired. What can I use to check that the file user had selected has an error and hence hasn't been added to the queue.

我能想到的唯一方法就是火灾一个添加事件并等待,看它是否没有触发添加事件然后出现问题。但是,我既不想长时间停止所有处理,也不想太快检查添加事件是否已被触发。

The only way I can think is it fires an "add" event and wait and see if it doesn't fire "added" event then there is a problem. However, I neither want to halt all processing for long time nor wants to check too quickly whether the "added" event has fired or not.

或者

有人可以指导如何从这个插件中抛出自定义事件(例如 fileaddfailedevent )吗?

Can anyone please guide on how to throw a custom event (e.g. fileaddfailedevent) from this plugin?

推荐答案

好吧,我找到了一种触发此要求的自定义事件的方法。如果有人想知道如何实现相同的话,请在此处发布答案。

Well, I figured out a way to trigger custom event for this requirement. Posting the answer here in case if anyone is wondering how to achieve the same.

我们需要更改的核心文件应位于js\jquery.fileupload -strong.js

The core file which we need to change should be located at "js\jquery.fileupload-ui.js"

添加内回调 add:function(e,data){最后一个监听器是})。失败(function(){。这应该在大约第120行(取决于版本)

inside the add call back add: function (e, data) { the last listener is }).fail(function () { . This should be at approximately line number 120 (depending on the version)

我们需要添加以下行:

that._trigger('addfileerror',e,data);

In that we need to add the following line :
that._trigger('addfileerror', e, data);

总结一下你的失败听众应如下所示:

To summarize your fail listener should look like below:

}).fail(function () {
if (data.files.error) {
    data.context.each(function (index) {
        var error = data.files[index].error;
        if (error) {
            $(this).find('.error').text(error);
        }
    });
    // added custom event to check whether any error has occured while adding the file.
    that._trigger('addfileerror', e, data); 
}
})


现在我们可以绑定一个监听器像任何其他侦听器一样检查此事件。


Now we can bind a listener just like any other listener to check for this event.

$('#fileupload').bind('fileuploadaddfileerror', function (e, data){
    console.log('Custom Error Event Fired');
});



希望这有助于某人。


Hope this helps someone.

更新:

即使文档没有明确提及,我刚刚学会了fileuploadprocessfail事件对于上述要求完全相同。

Even though the document doesn't mention it clearly, I have just learnt that "fileuploadprocessfail" event does exactly the same for the above requirement.

这篇关于blueimp jquery-file-upload不会在不成功的添加上抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆