Single file upload, using plupload
posted March 28th 2010I’m pretty happy with plupload for all my multi-upload needs. One thing that seems to be missing “out of the box”, is the ability to limit the number of queued files. It is possible, with a little event wrangling, to force plupload into allowing only a single upload.
The first thing you need to do is disable plupload’s “multi_selection” setting.
var uploader = new plupload.Uploader({
runtimes: 'gears,flash,silverlight,browserplus,html5',
multi_selection: false,
...
...
});
An important thing to note here. This doesn’t prevent queuing of multiple files, it only prevents multiple SELECTION of files. There are a few options to preventing queuing of multiple files. The one I’ve used below works for my needs, but there are other options.
uploader.bind('QueueChanged', function(up) {
if ( up.files.length > 0 && uploader.state != 2) {
uploader.start();
}
});
Basically, this is a bit of a cheat. Whenever we select a file, we check that we’re not already uploading and then automatically start the upload. At this point you could then choose to disable the uploader altogether, or in our case, display an icon for the uploaded file and give the user the option to replace it. I’ve seen other people use the event “FileAdded”, but this event looks like it should really be called “BeforeFileAdded”, as the newly added file doesn’t appear to be added to the “files” collection until after the event has run.
It’s worth noting that this method is really only appropriate if you’re using your own custom upload interface. Obviously the queuechanged event is triggered when adding AND removing files. Not to mention that our little cheat doesn’t actually clear the queue (although there is nothing stopping you doing that).
Comments
Leave a reply
Topics
Search
Archive
- August 2010 (4)
- July 2010 (2)
- May 2010 (2)
- April 2010 (3)
- March 2010 (1)
- February 2010 (3)
- January 2010 (3)
- November 2009 (1)
- October 2009 (2)
- September 2009 (4)
- August 2009 (5)
- May 2009 (2)
- April 2009 (1)
- March 2009 (4)
- November 2008 (1)
- December 2007 (2)
Comments(1)
Nice blog post, will look in to the event ordering the FilesAdded event should be fired after the files where added and the FilesRemoved should fire after the files where removed. Makes more sense.
We also had an single file widget from the beginning but we dropped it since it wasn’t that well written more of a small hack.