Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery file upload maxNumberOfFiles and getNumberOfFiles options

I am using jQuery file upload and setting maxNumberOfFiles to 1. However, it is allowing more than one file at a time. The documentation states...

The maxNumberOfFiles option depends on the getNumberOfFiles option, which is defined by the UI and AngularJS implementations.

There is no further documentation on the subject. getNumberOfFiles is just a function you can pass in.

Here is what I have...

$('#fileupload').fileupload({
                maxNumberOfFiles: 1,
                getNumberOfFiles: function () { return 1 }
});

Does anyone have the maxNumberOfFiles option working?

like image 762
John Livermore Avatar asked May 23 '13 21:05

John Livermore


2 Answers

I believe getNumberOfFiles was only added as an option to a recent version of jquery.fileupload-ui.js (8.2.1).

As an alternative, you can set the option singleFileUploads to false, and in the add callback you can throw errors if more than one file is added.

var maxFiles = 1;
$('#fileupload').fileupload({
    singleFileUploads: false,
    url: '/uploadUrl'
}).bind('fileuploadadd', function (e, data) {
        var fileCount = data.files.length;
        if (fileCount > maxFiles) {
            alert("The max number of files is "+maxFiles);
            return false; 
        }
    });
like image 101
mccannf Avatar answered Sep 21 '22 18:09

mccannf


I found just using a simple counter works well enough

var maxFiles = 1;
var counter=0;
$('#fileupload').fileupload({
url: '/uploadUrl',
add: function (e, data) {
        if(counter < maxFiles){
            counter++;
  }else return false;

    }
});
like image 25
DeanO Avatar answered Sep 17 '22 18:09

DeanO