Dropzone: prevent uploading of duplicate files

前端 未结 4 1016
孤独总比滥情好
孤独总比滥情好 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.

    0 讨论(0)
  • 2021-01-04 07:44

    Add these simple lines of code:

    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())
                {
                    this.removeFile(file);
                }
            }
        }
    });
    
    0 讨论(0)
  • 2021-01-04 07:45

    I'm checking from the server if the file is duplicate and then returning the error to dropzone,ee below:

     $targetPath = '/tmp/my_dropzone_files';
     $image_name = $_FILES['file']['name'];
     $targetFile =  $targetPath . '/' . $image_name; 
    
            $file_exists = file_exists ( $targetFile );
    
            if( !$file_exists ) //If file does not exists then upload
            {
                move_uploaded_file( $tempFile, $targetFile );
            }
            else //If file exists then echo the error and set a http error response
            {
                echo 'Error: Duplicate file name, please change it!';
                http_response_code(404);
            }
    
    0 讨论(0)
  • 2021-01-04 07:46

    The following solution helped me:

    this.on('addedfile', function(file) {
        setTimeout(function() { 
            $(".dz-file-preview").remove(); // removes all files except images
        }, 3000);
    });
    
    0 讨论(0)
提交回复
热议问题