Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery File Upload Plugin: trigger an event when all files are uploaded

I use the jQuery File Upload Plugin (http://blueimp.github.io/jQuery-File-Upload/) to manage my file uploads. It works pretty well.

I can detect when each individual file is uploaded and (for example) display a message.

But I would like to detect when every files are uploaded to display a final message.

How to do such thing?

Below is my actual implementation:

    $('#fileupload').fileupload({
        url: "api/fileManager",
        dataType: 'json',
        maxFileSize: 100000000, // 100 MB for testing!
        dropZone: $(document.body)
    }).on('fileuploadchange', function (e, data) {
        // nothing here
    }).on('fileuploaddrop', function (e, data) {
        // nothing here
    }).on('fileuploadsend', function (e, data) {
        // displaying the loading & progress bar
        $('#loading').show().html('<small><b>' + rscTransport.loading + '</b></small>');
        $('#progress').show();
    }).on('fileuploaddone', function (e, data) {
        // here this is called for each individual file
        if (typeof data.result != 'undefined') {
            ctxTransport.getDocumentsByType(data.result.typeId, documents);
            log('Fichier chargé avec succès.', '', true);
        } else {
            logError('Pas de réponse du serveur.');
        }            
    }).on('fileuploadfail', function (e, data) {
        alert('Error: ' + data.jqXHR.statusText + ' : ' + data.jqXHR.responseText);
        $('#loading').empty().hide();
        $('#progress').hide();
        $('#progress .bar').css('width', '0%');
    }).on('fileuploadprocessalways', function (e, data) {
        // nothing here
    }).on('fileuploadprogressall', function (e, data) {
        var progress = parseInt(data.loaded / data.total * 100, 10);
        $('#loading').html('<small><b>' + rscTransport.loading + progress + '% </b></small>');
        $('#progress .bar').css('width', progress + '%');
        if (data.loaded == data.total) {
            $('#loading').empty().hide();
            $('#progress').hide();
            $('#progress .bar').css('width', '0%');
        }            
    });
like image 779
Bronzato Avatar asked Jul 10 '13 14:07

Bronzato


2 Answers

It is recommended that you use the stop callback:

https://github.com/blueimp/jQuery-File-Upload/wiki/Options#stop

like image 151
ravibhagw Avatar answered Nov 15 '22 20:11

ravibhagw


One another way is using the plugin API to retrieve the number of active uploads in fileuploaddone callback:

var activeUploads = $('#fileupload').fileupload('active');

When all files are uploaded on the server, activeUploads == 1. You can then use it this way:

var $fileInput = $('#fileupload');

$fileInput.on('fileuploaddone', function(e, data) {
    var activeUploads = $fileInput.fileupload('active');
    if(activeUploads == 1) {
        console.info("All uploads done");
        // Your stuff here
    }
}

Take a look here: Number of active uploads JQuery-File-Upload.

Hope it helps!

like image 31
BoCyrill Avatar answered Nov 15 '22 21:11

BoCyrill