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

前端 未结 13 1283
隐瞒了意图╮
隐瞒了意图╮ 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:31

    1-set maxFiles Count "maxFiles: 1" 2- in maxfilesexceeded event Clear All file and add new file

    event: Called for each file that has been rejected because the number of files exceeds the maxFiles limit.

        var myDropzone = new Dropzone("div#yourDropzoneID", { url: "/file/post", 
     uploadMultiple: false, maxFiles: 1 });
    
    myDropzone.on("maxfilesexceeded", function (file) {
        myDropzone.removeAllFiles();
        myDropzone.addFile(file);
    });
    
    0 讨论(0)
  • 2020-11-28 04:31

    The problem with the solutions provided is that you can only upload 1 file ever. In my case I needed to upload only 1 file at a time (on click or on drop).

    This was my solution..

        Dropzone.options.myDropzone = {
            maxFiles: 2,
            init: function() {
                this.handleFiles = function(files) {
                    var file, _i, _len, _results;
                    _results = [];
                    for (_i = 0, _len = files.length; _i < _len; _i++) {
                        file = files[_i];
                        _results.push(this.addFile(file));
                        // Make sure we don't handle more files than requested
                        if (this.options.maxFiles != null && this.options.maxFiles > 0 && _i >= (this.options.maxFiles - 1)) {
                            break;
                        }
                    }
                    return _results;
                };
                this._addFilesFromItems = function(items) {
                    var entry, item, _i, _len, _results;
                    _results = [];
                    for (_i = 0, _len = items.length; _i < _len; _i++) {
                        item = items[_i];
                        if ((item.webkitGetAsEntry != null) && (entry = item.webkitGetAsEntry())) {
                            if (entry.isFile) {
                                _results.push(this.addFile(item.getAsFile()));
                            } else if (entry.isDirectory) {
                                _results.push(this._addFilesFromDirectory(entry, entry.name));
                            } else {
                                _results.push(void 0);
                            }
                        } else if (item.getAsFile != null) {
                            if ((item.kind == null) || item.kind === "file") {
                                _results.push(this.addFile(item.getAsFile()));
                            } else {
                                _results.push(void 0);
                            }
                        } else {
                            _results.push(void 0);
                        }
                        // Make sure we don't handle more files than requested
                        if (this.options.maxFiles != null && this.options.maxFiles > 0 && _i >= (this.options.maxFiles - 1)) {
                            break;
                        }
                    }
                    return _results;
                };
            }
        };
    

    Hope this helps ;)

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

    You can limit the number of files uploaded by changing in dropezone.js

    Dropzone.prototype.defaultOptions = { maxFiles: 10, }

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

    Nowell pointed it out that this has been addressed as of August 6th, 2013. A working example using this form might be:

    <form class="dropzone" id="my-awesome-dropzone"></form>
    

    You could use this JavaScript:

    Dropzone.options.myAwesomeDropzone = {
      maxFiles: 1,
      accept: function(file, done) {
        console.log("uploaded");
        done();
      },
      init: function() {
        this.on("maxfilesexceeded", function(file){
            alert("No more files please!");
        });
      }
    };
    

    The dropzone element even gets a special style, so you can do things like:

    <style>
      .dz-max-files-reached {background-color: red};
    </style>
    
    0 讨论(0)
  • 2020-11-28 04:42

    maxFiles: 1 does the job but if you also want to remove the additional files you can use this sample code taken from the Wiki page:

    How can I limit the number of files?

    You're in luck! Starting with 3.7.0 Dropzone supports the maxFiles option. Simply set it to the desired quantity and you're good to go. If you don't want the rejected files to be viewed, simply register for the maxfilesexceeded event, and remove the file immediately:

    myDropzone.on("maxfilesexceeded", function(file)
    {
        this.removeFile(file);
    });
    
    0 讨论(0)
  • 2020-11-28 04:43

    I achieved this a slightly different way. I just remove the old dropped file any time a new file is added. It acts as overwriting the file which was the user experience I was going for here.

    Dropzone.options.myAwesomeDropzone = {
      accept: function(file, done) {
        console.log("uploaded");
        done();
      },
      init: function() {
        this.on("addedfile", function() {
          if (this.files[1]!=null){
            this.removeFile(this.files[0]);
          }
        });
      }
    };
    
    0 讨论(0)
提交回复
热议问题