【问题标题】:HTML5 Upload via XHR fails in Chrome在 Chrome 中通过 XHR 上传 HTML5 失败
【发布时间】:2011-10-10 07:00:20
【问题描述】:

我正在使用 XHR 上传一个在 FF 中运行良好但在 Chrome 中失败的文件。

抛出一个错误,上面写着Upload failed: 0,这意味着xhr.status 以0 的形式返回——我不确定这是什么意思?没有记录其他状态。

//Check if we have XHR / File support
if (typeof File != "undefined" && typeof (new XMLHttpRequest()).upload != "undefined")
{
    var xhr = new XMLHttpRequest();
    xhr.upload.onprogress = function(e){
        if (e.lengthComputable){
            uploadStarted = true;

            var loaded = (e.loaded / e.total) * 100;

            ShowProgress(loaded);
        }
    };

    xhr.onreadystatechange = function(){
        if (xhr.readyState == 4){

            if (xhr.status == 200){
                uploadComplete();
            } else {
                alert("Upload failed: " + xhr.status);
            }

            console.log("status",xhr.status);
        }
    };

    var formElement = document.getElementById("configForm");

    xhr.open("POST", $("#configForm").attr('action') , true);
    xhr.send(new FormData(formElement));
}

【问题讨论】:

    标签: javascript html file-upload xmlhttprequest


    【解决方案1】:

    xhr.status == 0 表示存在 some network error。但是规范并没有说明如何找出错误所在。

    我建议将整个 xhr 结构记录到控制台 (console.log(xhr))。也许其他字段之一包含一些有价值的数据。

    如果失败,请检查服务器的错误日志。可能是服务器端出了点问题。

    【讨论】:

    • 您好,感谢您提供的信息。服务器端肯定没有错,使用 iframe 发布内容时也会失败,所以我认为这可能与 Chrome 或我们的内部代理有关?
    猜你喜欢
    • 1970-01-01
    • 2012-07-11
    • 2011-12-27
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多