3

我实现了 FineUploader,我想在上传所有文件后将我的客户端脚本连接到一个事件。那可能吗?

我的实现如下。只是想知道这是否是正确的方向。

    function init() {
    var uploader = new qq.FineUploader({
        element: document.getElementById('button-selectfiles'),
        request: {
            endpoint: '/Up/UploadFile'
        },

        callbacks: {
            onStatusChange: onFileUploadStatusChange
        }
    });
};

var uploads = 0;
function onFileUploadStatusChange(id, oldStatus, newStatus) {
    console.log(newStatus);

    if (newStatus === 'submitting') {
        uploads++;
    }

    if (newStatus === 'upload successful' || newStatus === 'upload failed') {
        uploads--;
    }

    if (uploads === 0) {

        console.log('done');
    }
}
4

2 回答 2

10

onComplete- 用于单个上传文件,如果您使用多个文件上传,只需使用onAllComplete

callbacks: {
    onAllComplete: function() {
        alert('done')
    }
}
于 2015-06-14T15:32:20.700 回答
5

您的onFileUploadStatusChange函数无法检查已取消的文件。

验证所有文件是否已上传的方法是通过 API 方法:getInProgressgetUploads. 如果我们有 0 次上传正在进行,0 次上传失败,那么我们可以安全地假设所有文件都已上传。如果您仍想在任何上传失败的情况下继续进行,您可能需要取消对上传失败的检查。onStatusChange我们在和onComplete回调期间检查要满足的这些条件。该onStatusChange事件应该只检查文件是否已被取消,因为这可能意味着所有其他文件都已完成,因此可以完成自定义操作。

注意:我已经调整了我对16989719的回答,以适用于非 jQuery Fine Uploader。

function init() {
    var uploader;

    function check_done() {
    // Alert the user when all uploads are completed.
    // You probably want to execute a different action though.
        if (allUploadsCompleted() === true) {
            window.alert('All files uploaded');
        } 
    }

    function allUploadsCompleted() {
        // If and only if all of Fine Uploader's uploads are in a state of 
        // completion will this function fire the provided callback.

        // If there are 0 uploads in progress...
        if (uploader.getInProgress() === 0) {
            var failedUploads = uploader.getUploads({ status: qq.status.UPLOAD_FAILED });
            // ... and none have failed
            if (failedUploads.length === 0) {
                // They all have completed.
                return true;
            }
        }        
        return false;
    }

    uploader = new qq.FineUploader({
        element: document.getElementById('button-selectfiles'),
        request: {
            endpoint: '/Up/UploadFile'
        },
        callbacks: {
            onStatusChange: function (id, oldStatus, newStatus) {
                // This will check to see if a file that has been cancelled
                // would equate to all uploads being 'completed'.
                if (newStatus === qq.status.CANCELLED) {
                    check_done();
                }     
            },
            onComplete: check_done
        }
    });
};
于 2013-10-09T14:40:37.190 回答