I am using jquery file upload to upload the files to the server . I want to restrict the user to upload maximum 6 files . I search the wiki jquery file upload but didnt find the parameter for it . Is there any way that i can restrict the user on number of uplaods
Asked
Active
Viewed 3.7k times
19
-
which jquery plugin you are using? generally inside callback function it will hold those files in some sort of json array whose size you can put in if() clause – duckduckgo Apr 15 '13 at 09:07
-
A why not working version: http://stackoverflow.com/questions/21347805/jquery-multiple-file-upload-limit-number-of-files-not-working – Ciro Santilli OurBigBook.com Nov 08 '14 at 09:33
5 Answers
33
Use maxNumberOfFiles
here is documentation :
$('#fileuploadbasic').fileupload({
maxNumberOfFiles: 6
});

Mohamed AbdElRazek
- 1,654
- 14
- 17
-
2This does not work in my case. I can select more than 6 files and it also uploads all selected files. – basZero May 09 '17 at 13:50
-
I found that sometimes the browser caches the .js file. Try refreshing the actual .js page and see if the changes take effect then. – BSUK Nov 27 '17 at 14:23
-
1This answer should be removed or at least clarified. Its no longer correct. That maxNumberOfFiles option only works if youre using the jquery fileupload with the pre-made UI. Otherwise you have to implement it yourself. – M H Dec 08 '20 at 21:32
19
maxNumberOfFiles was not working for me so i did the following
$('#fileuploadbasic').fileupload({
change : function (e, data) {
if(data.files.length>=5){
alert("Max 5 files are allowed")
return false;
}
},
maxFileSize: 20000000,
acceptFileTypes: /(\.|\/)(jpe?g|png)$/i,
});

Muhammad Tahir
- 2,351
- 29
- 25
-
3
-
3I can't edit the answer for some reason, but if you replace the "change" handler by "submit", it will work. That did the trick for me and "change" wasn't working either :) – NaturalBornCamper Mar 16 '17 at 09:29
-
@naturalBornCamper above code is for the time when we select file. it check the file before submitting. – Muhammad Tahir Jun 04 '17 at 19:08
2
You can limit the uploading files by the "Uploadhandler.php" file .change the "max_number_of_files" option. works for me. But it only validates when you upload the file.

Hemantha
- 353
- 2
- 7
-
This is valid. A lot of the settings are available client side (via js) and server side (via PHP). I found it useful to duplicate on both, so that the user gets client feedback before the file has been uploaded, but a more secure level of validation takes place on the server also. – BSUK Nov 27 '17 at 14:25
-
Just remember to have some place to show this limit. In my case 100 uploads worked and the others failed, with no error message, just "An error ocurred". – Marcelo Agimóvel Mar 19 '18 at 02:41
0
If you are using the "Krajee" fileupload, then you will have to use
$('#fileuploadbasic').fileinput({
maxFileCount: 6
});
If set to 0, it means size allowed is unlimited. Defaults to 0.

TheCodeLord
- 417
- 6
- 12
-1
You can try:
$('#fileuploadbasic').fileupload({
//.....
paramName: 'your_input_name',
add : function (e, data) {
if(data.paramName != undefined) data.submit();
}
});

Artipixel
- 1,246
- 14
- 17