Dropzone: prevent uploading of duplicate files

前端 未结 4 1040
孤独总比滥情好
孤独总比滥情好 2021-01-04 06:57

I am using Dropzone. I\'d like to prevent the uploading of a file which already exists as thumbnail in Dropzone \"panel\". With uploading I mean not to allow a file with the

4条回答
  •  执念已碎
    2021-01-04 07:31

    Here is the solution I came to:

    Add these two options in your Dropzone initialization

    dictDuplicateFile: "Duplicate Files Cannot Be Uploaded",
    
    preventDuplicates: true,
    

    and add one more prototype function and re-implement your dropzone addFile prototype function above dropzone initialization like this:

    Dropzone.prototype.isFileExist = function(file) {
          var i;
          if(this.files.length > 0) {
            for(i = 0; i < this.files.length; i++) {
              if(this.files[i].name === file.name 
                && this.files[i].size === file.size 
                && this.files[i].lastModifiedDate.toString() === file.lastModifiedDate.toString())
               {
                   return true;
               }
            }
          }
          return false;
        };
    
    Dropzone.prototype.addFile = function(file) {
          file.upload = {
            progress: 0,
            total: file.size,
            bytesSent: 0
          };
          if (this.options.preventDuplicates && this.isFileExist(file)) {
            alert(this.options.dictDuplicateFile);
            return;
          }
          this.files.push(file);
          file.status = Dropzone.ADDED;
          this.emit("addedfile", file);
          this._enqueueThumbnail(file);
          return this.accept(file, (function(_this) {
            return function(error) {
              if (error) {
                file.accepted = false;
                _this._errorProcessing([file], error);
              } else {
                file.accepted = true;
                if (_this.options.autoQueue) {
                  _this.enqueueFile(file);
                }
              }
              return _this._updateMaxFilesReachedClass();
            };
          })(this));
        };
    

    You can also modify your drozone file if you want.

提交回复
热议问题