netdjw netdjw - 2 months ago 67
Javascript Question

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

I'm using Dropzone.js to upload files. Now I want to check a filed is filled or not, and if it isn't then cancel the upload.

My HTML code:

<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:

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?

Answer

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);
            }
        }
    }
});