1

I'm using jquery fileupload without ui.

I want to control max files uploaded.

For that I'm checking data.files.length in add event and If I select and upload 8 files I get 8 times 1 not once 8

$('#fileupload').fileupload({
   dataType: 'json',
   add: function(e, data) {
      console.log(data.files.length); //8 times 1 - not 8
   }
});

I can make something like this:

add: function(e, data) {
   files+=data.files.length;
   if (files>5) {
      jqXHR = data.submit();
      jqXHR.abort();
   }
},

But often 1 file manages to upload :/

How to limit number of uploaded files without using ui (and all depencies like video, image, audio and so on)?

piernik
  • 3,507
  • 3
  • 42
  • 84
  • possible duplicate of [jquery file upload restricting number of files](http://stackoverflow.com/questions/16011200/jquery-file-upload-restricting-number-of-files) – Dave Bryand Jun 26 '14 at 14:06
  • It's not duplicate because I don't want to use ui or angular – piernik Jun 26 '14 at 14:09
  • Sorry about that, I pasted the wrong duplicate link: http://stackoverflow.com/questions/10056062/get-upload-file-count – Dave Bryand Jun 26 '14 at 14:23

1 Answers1

2

You'll need to listen to the fileuploadadd event and use that to keep track of the number of files added. Something along the lines of this:

var allowedFilesCount = 8;
var addedFilesCount = 0;

$('#fileupload').bind('fileuploadadd', function (e, data) {
  addedFilesCount++;
});

$('#fileupload').fileupload({
   dataType: 'json',
   add: function(e, data) {
     if (addedFilesCount <= allowedFilesCount){
       alert("allowed");
     } else {
       alert("at max");
     }
   }
});
Dave Bryand
  • 601
  • 6
  • 11