简体   繁体   English

如果条件不满足,如何在Dropzone.js中取消/中止上传?

[英]How to cancel/abort upload in Dropzone.js if condition is not fulfilled?

I'm using Dropzone.js to upload files. 我正在使用Dropzone.js上传文件。 Now I want to check a filed is filled or not, and if it isn't then cancel the upload. 现在我想检查一个字段是否已填写,如果不是,则取消上传。

My HTML code: 我的HTML代码:

<input type="text" name="title" id="title" placeholder="Type the title of the album">
<form action="file-upload.cgi" enctype="multipart/form-data" class="dropzone" id="dropzoneform"></form>

My jQuery code: 我的jQuery代码:

Dropzone.autoDiscover = false;
$('.dropzone').dropzone({
    acceptedFiles: 'image/jpeg',
    init: function() {
        this.on('sending', function() {
            if ( $('#title').val().length > 0 ) {
                // just start automatically the uploading
            } else {
                // need a code to cancel the uploading
            }
        }
    }
});

How can I cancel the uploading when the #title 's length is 0? #title的长度为0时,如何取消上传?

The accepted answer works if you want to remove the file completely; 如果要完全删除文件,则接受的答案有效; however, if you want to give the user the option to try and submit the same file again without having to re-add it to dropzone, you need to change a few things about how files are processed. 但是,如果您希望为用户提供尝试再次提交相同文件的选项而无需将其重新添加到dropzone,则需要更改有关文件处理方式的一些内容。

First, you need to set the autoQueue property to false when you initialize your dropzone: 首先,初始化dropzone时需要将autoQueue属性设置为false:

$('.dropzone').dropzone({
    autoQueue: false,
    /* The rest of your configuration options */
});

This prevents Dropzone from automatically trying to upload every file right after it's added by the user. 这可以防止Dropzone在用户添加后立即自动尝试上传每个文件。 This means that now you have to actually enqueue the files yourself programmatically, using the following function: 这意味着现在您必须使用以下函数以编程方式自己将文件排入队列:

 var yourDropzone = $('.dropzone');
 yourDropzone.on("addedfile", function(file) {
     if (/* some condition is met by your file */) {
         yourDropzone.enqueueFile(file);
     }
 }

This way, whenever the user selects a file for uploading, we can check if some arbitrary condition is met. 这样,无论何时用户选择要上载的文件,我们都可以检查是否满足某些任意条件。 Only when this is true, we enqueue the file for uploading to the server. 只有在这种情况下,我们才会将文件排入上传到服务器。

The advantage of this approach is that now we can actually choose when the file gets uploaded, rather than trying to remove it from the queue while Dropzone is trying to send the enqueued files. 这种方法的优点是,现在我们可以实际选择何时上传文件,而不是在Dropzone尝试发送排队文件时尝试将其从队列中删除。

This also allows for other fancier features; 这也允许其他更高级的功能; for instance, adding a button for each file that lets the user choose when to try and upload it. 例如,为每个文件添加一个按钮,让用户选择何时尝试上传它。

What about this 那这个呢

Dropzone.autoDiscover = false;
$('.dropzone').dropzone({
    acceptedFiles: 'image/jpeg',
    init: function() {
        var that = this;
        that.on('sending', function(file) {
            if ( $('#title').val().length <= 0 ) {
                that.removeFile(file);
            }
        }
    }
});

If the solution of the accepted answer throw this error : 如果接受的答案的解决方案抛出此错误:

InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable InvalidStateError:尝试使用不可用或不再可用的对象

Try 尝试

myDropzone.on("addedfile", function(file) {
if (/* some condition is NOT met by your file */) {
    myDropzone.removeFile(file);
}});

It's a mix between accepted answer and Protossoario's answer. 这是接受的答案和Protossoario的答案之间的混合。 Without queuing the file. 排队文件。 The accepted answer is actually throwing this js error in my config. 接受的答案实际上是在我的配置中抛出这个js错误。

You can also catch the event "addedfile" when you declare your dropzone, this happens before sending anything. 当您声明dropzone时,您也可以捕获事件“addedfile”,这在发送任何内容之前发生。
Example : 示例:

jQuery("#media-uploader").dropzone({
    url: your_posting_url,
    acceptedFiles: 'image/jpeg',
    addedfile:function (file) {
        if( your condition ){
            // okay !
        } else {
            // not okay !
            this.removeFile(file);
        }
    },
    ... etc ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM