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

时间:2016-02-29 09:21:01

标签: javascript jquery dropzone.js

所以我有一个带有Dropzone的表单,加上我要提交的另一个textarea - 如果我插入一个超大文件或者太多,我会得到"超大"预览容器中的错误等。但是,当按钮单击表单提交时,表单将继续处理(由于我的监听器)。如果两个文件的文件大小都正确并且不超过最大文件限制,我该如何才能提交?我无法看到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();

        }); 



    }

};

});

1 个答案:

答案 0 :(得分:3)

如果要验证dropzone错误,可以检查它包含的被拒绝文件。

一个简单示例(仅限一个文件,maxfilesize为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();
    }
});

我希望它对你有用。

此致,Yecid