How to limit the number of dropzone.js files uploaded?

前端 未结 13 1258
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 04:28

Depending on the use case, how do I constrain the number of files that dropzone.js will allow?

For example, I might need to only allow 1, 2, or 4 files uploaded.

相关标签:
13条回答
  • 2020-11-28 04:43

    I'd like to point out. maybe this just happens to me, HOWEVER, when I use this.removeAllFiles() in dropzone, it fires the event COMPLETE and this blows, what I did was check if the fileData was empty or not so I could actually submit the form.

    0 讨论(0)
  • 2020-11-28 04:45

    it looks like maxFiles is the parameter you are looking for.

    https://github.com/enyo/dropzone/blob/master/src/dropzone.coffee#L667

    0 讨论(0)
  • Dropzone.options.dpzSingleFile = {
        paramName: "file", // The name that will be used to transfer the file
        maxFiles: 1,
        init: function() {
            this.on("maxfilesexceeded", function(file) {
                this.removeAllFiles();
                this.addFile(file);
            });
        }
    };
    
    0 讨论(0)
  • 2020-11-28 04:51

    You can also add in callbacks - here I'm using Dropzone for Angular

    dzCallbacks = {
        'addedfile' : function(file){
            $scope.btSend = false;
            $scope.form.logoFile = file;
        },
        'success' : function(file, xhr){
            $scope.btSend = true;
            console.log(file, xhr);
        },
        'maxfilesexceeded': function(file) {
             $timeout(function() { 
                file._removeLink.click();
            }, 2000);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 04:55

    Why do not you just use CSS to disable the click event. When max files is reached, Dropzone will automatically add a class of dz-max-files-reached.

    Use css to disable click on dropzone:

    .dz-max-files-reached {
              pointer-events: none;
              cursor: default;
    }
    

    Credit: this answer

    0 讨论(0)
  • 2020-11-28 04:57

    I thought that the most intuitive single file upload process was to replace the previous file upon a new entry.

      $(".drop-image").dropzone({
        url: '/cart?upload-engraving=true',
        maxFiles: 1,
        maxfilesexceeded: function(file) {
            this.removeAllFiles();
            this.addFile(file);
        }
    })
    
    0 讨论(0)
提交回复
热议问题