9

我使用 jQuery 文件上传插件 ( http://blueimp.github.io/jQuery-File-Upload/ ) 来管理我的文件上传。它工作得很好。

我可以检测到每个单独的文件何时上传并(例如)显示一条消息。

但我想检测每个文件何时上传以显示最终消息。

怎么做这样的事情?

下面是我的实际实现:

    $('#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%');
        }            
    });
4

3 回答 3

9

建议您使用停止回调:

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

于 2013-07-10T14:26:23.530 回答
4

另一种方法是使用插件 API 在 fileuploaddone 回调中检索活动上传的数量:

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

当所有文件都上传到服务器时,activeUploads == 1。然后您可以这样使用它:

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
    }
}

看看这里:活跃上传数 JQuery-File-Upload

希望能帮助到你!

于 2016-03-25T13:21:10.570 回答
0

作为插件选项和回调的替代方案,我用纯 jQuery 解决了同样的问题。我在调用“全部上传”按钮的 javascript 函数中有以下几行:

    $(document).ajaxStop(function(){
        alert('All uploads done');
        $(this).unbind("ajaxStop");
    });
于 2015-08-28T10:57:30.990 回答