Dropzone max file only one and remove the second file

6.9k views Asked by At

I just use my code like this

Dropzone.options.attachkyc = {
            maxFiles: 1,
            accept: function(file, done) {
            console.log("uploaded");
            done();
            },
            init: function() {
                this.on("maxfilesexceeded", function(file){
                    alert("No more files please!");
                });
            },
            addRemoveLinks: true,
            removedfile: function(file) {
                var name = file.name;        
                $.ajax({
                    type: 'POST',
                    url: host+'upload/unfile',
                    data: "id="+name,
                    dataType: 'html'
                });
                var _ref;
                return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;   

                //console.log();
            }
        };

When i upload second file is show alert "No more files please!", well is working taht file not uploaded, but my problem is, that second file i'm add it still show on my dropzone. My question is how i'm remove second file automaticly after i show the alert??

3

There are 3 answers

3
Jenish On BEST ANSWER

if you want to remove max file extended file you just have to use maxfilesexceeded method of dropzone

    init: function() {
         myDropzone.on("maxfilesexceeded", function (file) {
                this.removeFile(file);
            });
    },

or you can also used one other method too

  init: function() {
  myDropzone.on("addedfile", function (file) {

                if (myDropzone.files.length === 1) {
                    alert("You can Select upto 1 Pictures for Venue Profile.", "error");
                    this.removeFile(file);
                }

            });
 },
1
Wolfzmus On

I finaly sovle my problem. on at

 init: function() {
            this.on("maxfilesexceeded", function(file){
                alert("No more files please!");
            });
        },

i change it into like this

init: function() {
            this.on("maxfilesexceeded", function(file){
                alert("No more files please!");
                this.removeFile(file);
            });
        },

is work perfectly i want.

0
Gino On

If you are looking to keep the newly added file and remove the first file that was added you can do something like what is below:

 init: function() {
   myDropzone.on("maxfilesexceeded", function(file) {
     this.removeFile(this.files[0]);
  });
 },

You could obviously update the index of this.files to access other files in the array as well.