javascript - 在 Chrome 中通过 XHR 上传 HTML5 失败

标签 javascript html file-upload xmlhttprequest

我正在使用 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));
}

最佳答案

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

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

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

关于javascript - 在 Chrome 中通过 XHR 上传 HTML5 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6777571/

相关文章:

javascript - 如何在JavaScript中将音频添加到10秒倒计时

jquery - 如何在 jquery 中使用 or 和 运算符?

java - 在 Struts 中上传服务器 webcontent/upload 文件夹内的文件

JavaScript 转换 - 为什么比较 "5"> "300"返回 true?

javascript - 如何在 JavaScript/CoffeeScript 中合并数组?

javascript - 将 Unicode 字符插入 JavaScript

javascript - 带有 rails carrierwave/s3 的 jquery fileupload

ios - 在 Swift 中使用多部分表单数据 iOS 上传图像

javascript - 主干 View 在初始化方法中自动提供选项对​​象

css - Firefox 中的 Div 位置问题