问题
So I'm using PLUpload to, well, upload files. However, I've run into a problem. I'm trying to fire an event when the queue is completed. I'm close, but not quite there. See here:
$(function() {
var files_remaining = 0;
// Setup flash version
$("#flash_uploader").pluploadQueue({
// General settings
runtimes : 'flash',
url : 'blah.php',
max_file_size : '200mb',
chunk_size : '1mb',
// Flash settings
flash_swf_url : 'plupload.flash.swf'
});
var uploader = $("#flash_uploader").pluploadQueue();
uploader.bind('FilesAdded', function(up, file, res)
{
files_remaining++;
});
uploader.bind('FileUploaded', function(up, file, res)
{
files_remaining--;
if (files_remaining == 0)
{
alert('Complete!');
}
});
});
Notice the last two .bind()
uses. Basically, I'm trying to update a variable to figure out how many files have been uploaded, and then figure out (based on that) when the queue has been completed. However, as you likely know, FilesAdded isn't run on a per-file basis, but is instead run when any amount of files are added to the queue. If three files are added, FilesAdded is only called once. That means this attempt only works if one file is uploaded at a time.
So, my question is this: what is the best way to get the number of files remaining in the queue, or ultimately (even better) call an event everytime the queue finishes? I know lots of people have this problem, but I haven't found a single solution I've been able to get work!
Thanks!
回答1:
$(function() {
var files_remaining = 0;
// Setup flash version
$("#flash_uploader").pluploadQueue({
// General settings
runtimes : 'flash',
url : 'blah.php',
max_file_size : '200mb',
chunk_size : '1mb',
// Flash settings
flash_swf_url : 'plupload.flash.swf'
});
var uploader = $("#flash_uploader").pluploadQueue();
uploader.bind('QueueChanged', function(up, files)
{
files_remaining = uploader.files.length;
});
uploader.bind('FileUploaded', function(up, file, res)
{
files_remaining--;
if (files_remaining == 0)
{
alert('Complete!');
}
});
});
回答2:
In FileUploaded event check up.total.queued
uploader.bind('FileUploaded', function(up, file, res)
{
if (up.total.queued == 0)
{
alert('Complete!');
}
});
来源:https://stackoverflow.com/questions/11315855/plupload-get-files-remaining