jquery Dropzone.js with duplicate file check using md5

Asked

Viewed 387 times

0

If you use a simple check in the "addedfile" event of the "Dropzone" component it works normally by sending a file, or adding several at a time and also selecting several and sending at once.

myDropzone.on("addedfile", function (file) {
        if (this.files.length) {
            var _i, _len;
            for (_i = 0, _len = this.files.length; _i < _len - 1; _i++) // -1 to exclude current file
            {
                if (this.files[_i].name === file.name && this.files[_i].size === file.size && this.files[_i].lastModifiedDate.toString() === file.lastModifiedDate.toString())
                {
                    alert('O arquivo ' + file.name + ' já foi enviado');
                    this.removeFile(file);
                }
            }
        }
    });

But trying to implement using md5 to check the files works if sending only one file, or sending one by one, but if selecting several and sending at once does not work.

myDropzone.on("addedfile", function (file) {
        if (this.files.length) {
            var _i, _len;
            for (_i = 0, _len = this.files.length; _i < _len - 1; _i++) // -1 to exclude current file
            {
                /**
                 * Cria um hash do arquivo e compara
                 */
                var reader = new FileReader();
                reader.readAsBinaryString(this.files[_i]);
                reader.onload = function () {
                    var hash = CryptoJS.MD5(CryptoJS.enc.Latin1.parse(btoa(reader.result)));
                    var reader2 = new FileReader();
                    reader2.readAsBinaryString(file);
                    reader2.onload = function () {
                        var hash2 = CryptoJS.MD5(CryptoJS.enc.Latin1.parse(btoa(reader2.result)));
                        if (hash.toString() === hash2.toString()) {
                            alert('O arquivo ' + file.name + ' já adicionado foi na lista');
                            myDropzone.removeFile(file);
                        }
                    };
                };
            }
        }
    });

I wanted to check if there are any duplicate files using md5.

  • Checking the file before using the MD5 worked normally.

1 answer

0


The process to check using md5 is very time consuming, so first check the size and then use md5.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.