How to check the maximum number of files before downloading?

Hi, I am using a plugin to upload files, and I need to check the number of files added before downloading the file ... Something like this

        $('#fileupload').bind('fileuploadadd', function (e, data) {
        filestoupload++;
        var numOfDivs = $('.request').size();
        if (numOfDivs < filestoupload) {
            upload = false; // Is just an example.
        }
    });
+5
source share
2 answers

This worked for me, in the definition of your file upload add beforesend and do a check

 var maxfiles=3;
 $('#fileupload').fileupload(({
    url: postFileUrl,
    submit: function (event, files) {
        //check for max files THIS IS WHERE YOU VALIDATE
        //console.log(files.originalFiles.length);
        var fileCount = files.originalFiles.length;
        if (fileCount > maxFiles) {
            alert("The max number of files is "+maxFiles);
            throw 'This is not an error. This is just to abort javascript';
            return false; 
        }
    }
  });

this throw is far from elegant, if you decide to implement it and find a way to avoid this, please let me know (at the moment it is necessary or it will display an error warning for each downloaded file)

+1
source

use data.files.length

 $('#fileupload').bind('fileuploadsubmit', function (e, data) {
    var fileCount = data.files.length;
    if (fileCount < filestoupload) {
        upload = false; 
    }
});
+1
source

All Articles