【问题标题】:Unable to abort upload on 'fileuploadchunkdone' event handler for blueimp file upload plugin无法在 blueimp 文件上传插件的“fileuploadchunkdone”事件处理程序上中止上传
【发布时间】:2013-11-15 05:49:09
【问题描述】:

一旦发生异常,我会尝试终止上传。 “fileuploadchunkdone”事件处理程序允许我为每个块捕获从服务器发送的“错误”json。理想情况下,我想在发生异常时立即中止上传。我需要对每个块进行此检查,因为我需要记录错误并在发生异常时显示错误代码。但是,我无法通过调用 data.jqXHR.abort() 来终止异常。它只是继续遍历每个块。有任何想法吗?这是该事件处理程序的代码:

.on('fileuploadchunkdone', function (e, data) {
                if (!data.result.success) {
                    running--;
                    var errMsg = "";
                    if (data.result != null) {
                        if (data.result.message != null) {
                            errMsg += data.result.message;
                        }
                        if (data.result.error != null)
                            errMsg += data.result.error;
                    }
                    layoutService.showErrorDialog(errMsg);

                    data.jqXHR = data.abort();
                    window.removeXHRfromPool(data);

                    if (running == 0) {
                        $('#dlgProgress').parent().hide();
                        $('#progresses').empty();
                    }
                }
        })

【问题讨论】:

  • 这里有类似的问题。我试过在 chunksend 和 chunksuccess 事件中中止没有效果。还尝试在 setTimeout 中调用 abort,但这也无济于事。

标签: javascript jquery file-upload xmlhttprequest jqxhr


【解决方案1】:

如果 chunk-send 回调函数返回 false,则整个上传请求被中止。我正在添加一个自定义属性 abortChunkSend 以从 chunk-done 事件发出信号以取消下一个 chunk-send 事件。

例子,

.on("fileuploadchunksend", function (e, data)
{
    var abortChunkSend = data.context[data.index].abortChunkSend;
    if (abortChunkSend)
    {
        return false;
    }
})
.on("fileuploadchunkdone", function (e, data)
{
    if (data.result)
    {
        for (var index = 0; index < data.result.files.length; index++)
        {
            var file = data.result.files[index];
            if (file.error)
            {
                data.context[index].abortChunkSend = true;
            }
        }
    }
})

参考 fileuploadchunksend 上的 blueimp 文档。

【讨论】:

    猜你喜欢
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    相关资源
    最近更新 更多