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

FileFile UploadUploadLimitdropzone.js

File Problem Overview


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.

It's not uploadMultiple. Unfortunately, uploadMultiple only applies to the number of files handled per request.

File Solutions


Solution 1 - File

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]);
      }
    });
  }
};

Solution 2 - File

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>

Solution 3 - File

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);
	}
})

Solution 4 - File

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);
});

Solution 5 - File

Alternative solution that worked really well for me:

init: function() {
    this.on("addedfile", function(event) {
        while (this.files.length > this.options.maxFiles) {
            this.removeFile(this.files[0]);
        }
    });
}

Solution 6 - File

  1. Set maxFiles Count: maxFiles: 1
  2. In maxfilesexceeded event, clear all files and add a 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);
});

Solution 7 - File

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

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

Solution 8 - File

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

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

Solution 9 - File

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

Solution 10 - File

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 ;)

Solution 11 - File

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.

Solution 12 - File

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);
    }
}

Solution 13 - File

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);
        });
    }
};

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpydannyView Question on Stackoverflow
Solution 1 - FileoneiroisView Answer on Stackoverflow
Solution 2 - FilepydannyView Answer on Stackoverflow
Solution 3 - FileYuji 'Tomita' TomitaView Answer on Stackoverflow
Solution 4 - FileLeniel MaccaferriView Answer on Stackoverflow
Solution 5 - FilexaviertView Answer on Stackoverflow
Solution 6 - FileshabView Answer on Stackoverflow
Solution 7 - FilevipinlalrvView Answer on Stackoverflow
Solution 8 - FileNowellView Answer on Stackoverflow
Solution 9 - FileTimmy Von Heiss View Answer on Stackoverflow
Solution 10 - FileSimonDepelchinView Answer on Stackoverflow
Solution 11 - FileLucas GabrielView Answer on Stackoverflow
Solution 12 - FileMakahView Answer on Stackoverflow
Solution 13 - Fileragesh ppView Answer on Stackoverflow