24

I don't see an option in the plupload API docs on restricting the number of files uploaded, to any number, even 1.

Doc fail? or Feature fail? If it doesn't exist I'll be working on making that happen if anyone needs it..

JohnO
  • 1,889
  • 1
  • 12
  • 15

13 Answers13

20

Other way to do this:

 $(document).ready(function () {
    var uploader = new plupload.Uploader({
        ...
        multi_selection: false,
       ....
     });

Regards

ocuenca
  • 38,548
  • 11
  • 89
  • 102
20

It's a feature fail. I made a wrapper around the jQuery API and this is what I did to make it work for me. My code does some other business logic, but it should give you enough to set started.

Basically, bind to the FilesAdded event and call removeFile on the uploader object (if there are too many files). I think I added the 50ms timeout because it was giving me problems with the file not existing yet.

uploader.bind('FilesAdded', function (up, files) {
    var i = up.files.length,
        maxCountError = false;

    plupload.each(files, function (file) {

        if(uploader.settings.max_file_count && i >= uploader.settings.max_file_count){
            maxCountError = true;
            setTimeout(function(){ up.removeFile(file); }, 50);
        }else{
            // Code to add pending file details, if you want
        }

        i++;
    });

    if(maxCountError){
        // Too many files uploaded, do something
    }

});

max_file_count is something that I add to the pluploader instance when I create it.


Edit: I actually had to do two different ways for this. The above will only allow a person to upload a certain number of files (and generate an error otherwise).

This snippet works in a similar way - but will remove existing files and only upload the most recent:

uploader.bind('FilesAdded', function (up, files) {
    var fileCount = up.files.length,
        i = 0,
        ids = $.map(up.files, function (item) { return item.id; });

    for (i = 0; i < fileCount; i++) {
        uploader.removeFile(uploader.getFile(ids[i]));
    }

    // Do something with file details
});
Jonathon Bolster
  • 15,811
  • 3
  • 43
  • 46
  • As I mention in my comment on the other answer, it seems that `max_file_count` has been added in the jQuery UI widget. This still doesn't apply to my application, as I wanted a bucket that people could upload X number of files in (so it needed to take into account the existing uploaded files in the bucket) – Jonathon Bolster Nov 01 '11 at 09:44
  • thanks for a way that will work without having to fork their JS ;) – JohnO Nov 01 '11 at 16:38
  • 3
    The second way presented removed all my files, s.th. no files were left to work with. Running the loop only for `i < (fileCount - 1)` keeps only the most recently added file. – Adrian Lange Nov 12 '14 at 09:59
14

Based on Jonathon Bolster's second answer, I wrote this simpler snippet to restrict upload to the last selected file:

uploader.bind('FilesAdded', function(up, files) {
    while (up.files.length > 1) {
        up.removeFile(up.files[0]);
    }
});
JoaoPSF
  • 191
  • 1
  • 6
  • 1
    This seems to fire at the wrong point (before it's added), at least for the html5 frontend. The new file is added after this callback is triggerd. This results in limiting to 2 files, not one. – Joe Nov 30 '12 at 12:33
  • (Fixed with a zero setTimeout to append to the end of the event loop.) – Joe Nov 30 '12 at 13:05
  • This also doesn't work with drag/drop uploading, as you can add more then two files at a time. – devicenull Dec 19 '12 at 20:40
  • This should be up.files.length>0 since this fires before the file is actually added to the queue. ie the first time the length will be 0 the second time it will be one. You want to remove it already then – Murdock Apr 22 '14 at 20:50
6

You can use this max_file_count: 5 where 5 is the max number of upload count.

coder
  • 13,002
  • 31
  • 112
  • 214
  • 4
    This doesn't work. `max_file_count` is not a standard setting in plupload – Jonathon Bolster Nov 01 '11 at 09:29
  • I have added my current using code check out!And yes it's not in the examples on the site. – coder Nov 01 '11 at 09:31
  • Or else you can refer this question http://www.plupload.com/punbb/viewtopic.php?pid=5102 – coder Nov 01 '11 at 09:37
  • 3
    Ah ha - I stand corrected. Seems that the jQuery UI widget had `max_file_count` implemented (https://github.com/moxiecode/plupload/blob/master/examples/jquery/queue_widget.html). Also, please note that adding code or anything should be done by editing your answer (unless it's a completely different one) – Jonathon Bolster Nov 01 '11 at 09:39
  • Thanks for pointing me out.And yes I have implemented jquery UI widget – coder Nov 01 '11 at 09:44
  • 4
    If you are restricting to one file you may also want: 'multi_selection : false, // Turn off multi file select in "Open File" dialog' – RedYeti Oct 18 '12 at 15:19
3

Why not just

    if (files.length > 1) uploader.splice(1, files.length - 1);
8bitjoey
  • 31
  • 1
2

Try this. It works fine for me.

uploader.bind('FilesAdded', function(up, files) {

if(uploader.files.length > 1)
{
    uploader.removeFile(uploader.files[0]);
    uploader.refresh();// must refresh for flash runtime
}

. . . resto

Idea is to test num files in current uploader object. If length is greater than 1, then just use method uploader.removeFile. Notice that argument is files[0] which is not a file id, but complete file object.

({id:"p17bqh1v4dp42gdf1gan75p16tp3", name:"gnome-globe.png", size:48456, loaded:0, percent:0, status:1})

Best regards!

The iOSDev
  • 5,237
  • 7
  • 41
  • 78
Vladimir Djuricic
  • 4,323
  • 1
  • 21
  • 22
2

now you can disable multi-selection by setting multi_selection option to false

just like this

var uploader = new plupload.Uploader({
    runtimes : 'html5,flash',
    browse_button : 'button_id',
    multi_selection:false,  //disable multi-selection
...

official doc is here : http://www.plupload.com/documentation.php

chuck911
  • 745
  • 9
  • 11
2
    FilesAdded: function(up, files) {
        up.files.splice(0,up.files.length-1);
    },
    multi_selection: false,

use up.files, just files. files will always contain single item what we select from file browser. up.files is the actual list which we need to shrink to last selected file.

Sheikh Abdul Wahid
  • 2,623
  • 2
  • 25
  • 24
1

Allow only one file to be uploaded:

uploader.bind('FilesAdded', function(up, files) {
    $.each(files, function(i, file) {
        if(uploader.files.length!=1){uploader.removeFile(file); return;}
    });
});

Allow one file to be selected at once:

uploader.bind('FilesAdded', function(up, files) {
    $.each(files, function(i, file) {
        if(i){up.removeFile(file); return;}
    });
});

Allow one file to upload at once:

uploader.bind('FilesAdded', function(up, files) {
    $.each(files, function(i, file) {
        if(uploader.files.length!=1){uploader.removeFile(file); return;}
    });
});
uploader.bind('FileUploaded', function(up, file,response) {
    up.removeFile(file);
});
1

If you want to use a custom uploader, and upload one file at a time, no auto upload and have the last file added become the new file use this.

uploader.bind('FilesAdded', function(up, files) {
    // Clear the HTML
    $('#plupload-files').html('');

    // Plup does not offer before file added event
    if (up.files.length > 1) {
        up.splice(0, up.files.length);
        up.addFile(files[0])
        return;
    }

    // $.each(files, function(){....
}

We splice the whole array because plup already adds all the files to the queue and if you splice the queue like the accepted answer you will actually add one file everytime the user tries to add files, and if they try to add a new file singularly it will keep the old file at pos[0] in the files array,

Then we add the first file of the files that they tried to add. This way there is only ever one file in the queue and it is always the first file in the last group of files they tried to add.

ie.

Drag into plupload 'file1.jpg', 'file2.jpg', 'file3.jpg'

clear the whole queue, add back 'file1.jpg'

Drag into plupload 'file4.jpg', 'file5.jpg', 'file6.jpg'

clear the whole queue, add back 'file4.jpg'

Drag into plupload 'file99.jpg'

clear the whole queue, add back 'file99.jpg'

This allows you to manage custom plups if you only ever want to upload one file at a time. As stated the other answers only work once, or with auto start uploads.

Rob Quincey
  • 2,834
  • 2
  • 38
  • 54
Simon Ilett
  • 91
  • 1
  • 7
1

I realize this has been answered, but a solution I went with was to just use the QueueChanged callback:

QueueChanged: function(uploader) {
    // Called when queue is changed by adding or removing files
    //log('[QueueChanged]');
    if(uploader.files.length > 1) {
        uploader.files.splice(0, (parseInt(uploader.files.length) - 1));
    }
}

With this code, it only keeps the last selected file (in case the reason they chose again was because they chose the wrong file).

Rob Elsner
  • 823
  • 5
  • 16
James
  • 3,765
  • 4
  • 48
  • 79
0

Remove unnecessary files directly before uploading:

$('uploadfiles').onclick = function()
            {
                while (uploader.files.length > 1)
                {
                        uploader.removeFile(uploader.files[0]);
                }

                uploader.start();
                return false;
            };
0

After trying each of the solutions, I came up with the simplest of all -- which seems to work.

I'm using the core api and I have multi_selection set to false. Then, after the first file is selected (added) the second line of code, which I inserted into the FilesAdded event, hides the browse link. I don't think this can be done with the jquery widgets and I also have found that unless the upload link covers where the browse link is, it remains alive.

uploader.bind('FilesAdded', function(up, files) {
//line below hides button
document.getElementById("browse").style.display = "none";

var html = '';
plupload.each(files, function(file) {
html += '<li id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></li>';
});
document.getElementById('filelist').innerHTML += html;

});

JaneH
  • 359
  • 3
  • 15