sprintcar78a
sprintcar78a

Reputation: 199

Dropzone, how to not process queue if errors exist

So I have a form with Dropzone, plus another textarea, which I want to submit - if I insert an oversize file or too many I get the "oversize" error in the preview container, etc. BUT the form continues to process upon button clicking the form submit (due to my listener). How can I only submit if there file size is correct for both files and doesn't exceed max file limit? I can't see a Dropzone event for say "no errors" to add a click event listener - I think I'm close but semi stuck now, I have the below:

$(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();

        }); 



    }

};

});

Upvotes: 3

Views: 5745

Answers (1)

yecid
yecid

Reputation: 180

If you want to verify dropzone errors, you can check the rejected files that it contains.

A simple example (with restricted for only one file, maxfilesize to 1Mb and use version 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();
    }
});

I hope that it was useful to you.

Regards, Yecid

Upvotes: 5

Related Questions