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
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);
});
Remove unnecessary files directly before uploading:
$('uploadfiles').onclick = function()
{
while (uploader.files.length > 1)
{
uploader.removeFile(uploader.files[0]);
}
uploader.start();
return false;
};
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]);
}
});
Why not just
if (files.length > 1) uploader.splice(1, files.length - 1);
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!
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.