Check Duplicate file before Uploading

Checking if user is uploading duplicate file before uploading via dropzone.js

As we need to check this event immediately when user uploads the image via dropzon , that is why, we need to use event "addedfile" event from dropzone.

if we only want to check if file names are same :-

Below code will check, if file name uploaded already exists in dropzone-upload box. Remove it . It happens so fast, that you may not seeing being this getting added and remove.

myDropzone.on("addedfile", function(file) {
if (this.files.length) {
    var _i, _len;
    for (_i = 0, _len = this.files.length; _i < _len - 1; _i++) {
        if( this.files[_i].name === file.name ) {
            this.removeFile(file);
        }
    }
}
});

If we want to check file name may be same but file-size should be different :-

Below code will check, if file size are different even though file name is same.

myDropzone.on("addedfile", function(file) {
if (this.files.length) {
    var _i, _len;
    for (_i = 0, _len = this.files.length; _i < _len - 1; _i++) {
        if(this.files[_i].name === file.name && this.files[_i].size === file.size) {
            this.removeFile(file);
        }
    }
}
});

Bang! what if file names were same and file size luckily are same too ? Let's see final third solution :-

If we want to check when file was lastModified :-

There are almost no change that file last modified date could be same too for 2 different file with same name and same size.

myDropzone.on("addedfile", function(file) {
if (this.files.length) {
    var _i, _len;
    for (_i = 0, _len = this.files.length; _i < _len - 1; _i++) {
        if(this.files[_i].name === file.name && this.files[_i].size === file.size && this.files[_i].lastModifiedDate.toString() === file.lastModifiedDate.toString()) {
            this.removeFile(file);
        }
    }
}
});

Go ahead and use it.

If you have better solution. Please write a blog with tag "dropzone duplicate file" and it will get auto-added in related file or you can send me message to add you author on Dropzone course.


Loading ...

Related Results :

  1. DropzoneJS with Laravel
  2. Check Duplicate file before Uploading
Note :
  • Related Posts are generally User Blog posts.
  • or Other tutorials from other networks of w3clan.com.
  • Any registered user can create related posts based on search term tags.

About the Author