1

I'm using Plupload to upload files to my website. I'm trying to limit the number of files that can be selected before uploading. I tried setting multi_selection to false which partially works. However, if you click on 'Select files' and choose a file via the file explorer, wait for the file explorer to close, then click on 'Select files' and do the same again, you are able to select multiple files.

How can I allow just one file to be selected before uploading?

Here is a demo of the code I'm using: http://jsfiddle.net/5j8c05j9/

henrywright
  • 10,070
  • 23
  • 89
  • 150
  • Take a look at this question: http://stackoverflow.com/questions/15513689/jquery-plupload-restrict-number-of-uploads. – Luís Cruz Nov 09 '14 at 17:55
  • 1
    Thanks @milz - I'm not entirely sure how to use that answer. Is there any chance you could give an example? – henrywright Nov 09 '14 at 19:24

1 Answers1

2

Based on the answers in this question: jQuery Plupload restrict number of uploads.

You can add the following property to plupload:

max_files: 1,

and then change the FilesAdded function, as such:

FilesAdded: function(up, files) {
    plupload.each(files, function(file) {
        // if there are more files than the allowed  
        if (up.files.length > up.settings.max_files) {
            // display alert message
            alert('Cannot send more than ' + up.settings.max_files + ' file(s).');

            // here you can also hide the "Select Files" button with the following code
            //$(up.settings.browse_button).hide();

            // cancel adding files. break each.
            return false;
        }
        document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>';
    });
},

Take a look at your updated jsfiddle

Community
  • 1
  • 1
Luís Cruz
  • 14,780
  • 16
  • 68
  • 100