9

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%');
        }            
    });
1

3 Answers 3

9

It is recommended that you use the stop callback:

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

Sign up to request clarification or add additional context in comments.

1 Comment

For some reason this event also seems to fire after each individual image is uploaded, rather than when the whole batch is complete.
4

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!

2 Comments

If you use the callback version of done, you need to use $(event.target) instead of $fileInput.
Ops, this leaded to a problem if some of our customers. If they pick for example 20 pictures, even before it ends, activeUploads trigger "All uploads done" and uploads are stoped. Don't know why, no error in console, no way to simulate in other machines.
0

As an alternative to the plugin options and callbacks, I've solved the same issue with pure jQuery. I have the following lines in a javascript function that calls the "upload all" button:

    $(document).ajaxStop(function(){
        alert('All uploads done');
        $(this).unbind("ajaxStop");
    });

1 Comment

I upvoted you a while ago, but for some reason some times it seems that fails.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.