【问题标题】:data.context is empty on fileuploadprogress event of jQuery File UploaderjQuery File Uploader 的 fileuploadprogress 事件中的 data.context 为空
【发布时间】:2013-10-07 14:55:11
【问题描述】:

我遇到了jQuery File Uploader (blueimp) 的问题:

我的实现是完全定制的。我使用图像预览和验证。

我的问题是,即使我在 fileuploadadd 事件中定义 data.context,当 fileuploadprogress 被触发时它仍然是空的。

$('fileinput-element').fileupload({
    url: '/__module/uploadModule/receive',
    type: 'post',
    acceptFileTypes: acceptedFileRegEx,
    dataType: 'json',
    'dropZone': $(dropZone),
    paramName: 'files',
    autoUpload: false,
}).on('fileuploadadd', function (e, data) {

    $.each(data.files, function (index, file) {

        // NOTE: This function call returns a clone of an element that i use as template for upload list items. Works fine.
        var component=uploadModule.getDynamicComponent('listItemContainer');

        // Filling the template (code omitted due to better readabilty)

        // Appending the component to my #uploadList and assigning that to data.context
        data.context = component.appendTo('#uploadList');

    });

    // For manual submittion later on, i fill an array with the data objects from each event call
    dataList.push(data);

}).on('fileuploadprocessalways', function(e,data){

    var index = data.index,
        file = data.files[index],
        node = $(data.context.children()[index]);

    // Further code ommitted due to readability. Here i apply file.preview and (potential) file.error to my list item that is now stored in the variable node. This works fine. My list renders perfectly well.

}).on('fileuploadprogress', function(e, data){

    // PROBLEM: data.context is EMPTY ! Why???
    console.log(data.context);

});

这就是我在单击开始上传按钮时提交所有内容的方式:

$(dataList).each(function(){
    this.submit();
});

// This works well - the uploads are being submitted and sent to the server.

我已经进行了 1.5 天的广泛解决方案搜索、测试、试错和调试。我只是无法解决这个问题。

我做错了什么?

请注意:如果我在add 事件回调中执行data.submit(),那么当进度事件触发时data.context 会正确填充,但我不再剩下手动启动整个上传队列的选项。

【问题讨论】:

    标签: javascript jquery scope jquery-file-upload


    【解决方案1】:

    这个问题的解决方案相当出人意料:

    我对文件上传组件和插件的加载顺序是:

    jquery
    jquery-ui (for the widget
    load-image.js
    iframe-transport.js
    fileupload.js
    fileupload-process.js
    fileupload-image.js
    fileupload-validate.js
    fileupload-ui.js
    

    最后一个(fileupload-ui.js)是陷阱。我以为我必须将它包含在我的上传器实现中,但我没有。看来我误解了它的用途。

    在我删除它的那一刻,我的脚本/实现开始工作,data.context 现在可以正确填充。

    【讨论】:

    • 哦,谢谢鲍勃,我不是唯一一个遇到这个问题的人......现在我需要那个文件吗?为什么要清理.context
    • 我遇到了这个确切的问题,您的解决方案对我有用。有趣的是,在另一个站点上,我包含了这个文件并且没有任何问题。可能此错误背后的原因可能是只有在使用 jQuery-ui 时才必须包含 -ui 文件但是,在它工作的站点上我有 JQuery2.0.3,而在另一个我有 jQuery2.1.0
    猜你喜欢
    • 2014-01-12
    • 2013-09-22
    • 1970-01-01
    • 2013-07-18
    • 2015-11-09
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    • 2015-06-05
    相关资源
    最近更新 更多