Dropzone,如果存在错误,如何不处理队列



所以我有一个带有 Dropzone 的表单,以及另一个文本区域,我想提交 - 如果我插入一个超大文件或太多文件,我会在预览容器中收到"超大"错误等。 但是在单击表单提交按钮后,表单会继续处理(由于我的听众)。如何仅在两个文件的文件大小正确且不超过最大文件限制的情况下提交?我看不到说"没有错误"以添加点击事件侦听器的 Dropzone 事件 - 我想我已经接近了,但现在半卡住了,我有以下内容:

$(function() {
var minImageWidth = 300, minImageHeight = 300;
Dropzone.options.jobApplicationUpload = {
    autoProcessQueue: false,
    addRemoveLinks: true,
    uploadMultiple: true,
    paramName: 'file',
    previewsContainer: '.dropzone-previews',
    acceptedFiles: '.pdf, .doc, .docx',
    maxFiles: 2,
    maxFilesize: 2, // MB   
    dictDefaultMessage: '',
    clickable: '.fileinput-button',
    accept: function(file, done) {            
        done();
    },
    // The setting up of the dropzone           
    init: function() {
        var myDropzone = this;              
        // First change the button to actually tell Dropzone to process the queue.
        this.element.querySelector("button[type=submit]").addEventListener("click", function(e) {
            // Make sure that the form isn't actually being sent.
            if(myDropzone.files.length > 0) {
                $('#job-application-container').hide();
                $('#spinner-modal').modal('show');
                $('#spinner-modal p').html('<b>Sending your application,</b> please wait...</p>');  
                e.preventDefault();
                e.stopPropagation();
                myDropzone.processQueue(); 
            }
        });
        this.on("success", function(files, response) {

        // Gets triggered when the files have successfully been sent.
        // Redirect user or notify of success.
            $('#job-application-container').hide();
            console.log('okay' + response);
            localStorage['success'] = 'test';
            location.reload();
        }); 

    }
};

});

如果要验证拖放区错误,可以检查其中包含的被拒绝的文件。

一个简单的例子(限制只有一个文件,最大文件大小为1Mb,使用版本4.3.0):

var myDropzone = new Dropzone("div#myDropzone", {
    url: "toLoadUrl",
    autoProcessQueue: false,
    uploadMultiple: false,
    maxFiles: 1,
    maxFilesize: 1,
    init: function() {
        this.on("addedfile", function() {
            if (this.files[1]!=null){
                this.removeFile(this.files[0]);
            }
        });
    }
});
$('#toServerButton').on('click',function(e){
    e.preventDefault();
    if (myDropzone.files.length == 0){
        alert("You should be select any file");
    } else if(myDropzone.getRejectedFiles().length > 0) {
        alert("The attached file is invalid");
    } else {
        myDropzone.processQueue();
    }
});

我希望它对你有用。

问候,耶西德

最新更新