Ajax XmlHttpRequest使用“内容类型:多部分/表单数据”破坏Firefox 3.6中的标题

Ajax XmlHttpRequest使用“内容类型:多部分/表单数据”破坏Firefox 3.6中的标题,ajax,upload,file-upload,xmlhttprequest,header,Ajax,Upload,File Upload,Xmlhttprequest,Header,我正在开发多个ajax uloader。在主流浏览器Chrome 6、Firefox 4中运行良好。但在Firefox3.6中,我必须手动创建要发送的输出字符串,因为这个浏览器不支持FormData对象 我遵循了很多教程,尤其是。作者通知要发送的正文标题和内容的正确设置。我仔细地遵循了这个建议,但Firefox3.6失败了 这是通过提交简单静态表单捕获的标题和正文的正确设置: , 这就是我使用Firefox的xhr对象提交相同数据时得到的结果: , 正如您所看到的,xhr的头已经损坏。这将导致文

我正在开发多个ajax uloader。在主流浏览器Chrome 6、Firefox 4中运行良好。但在Firefox3.6中,我必须手动创建要发送的输出字符串,因为这个浏览器不支持FormData对象

我遵循了很多教程,尤其是。作者通知要发送的正文标题和内容的正确设置。我仔细地遵循了这个建议,但Firefox3.6失败了

这是通过提交简单静态表单捕获的标题和正文的正确设置: ,

这就是我使用Firefox的xhr对象提交相同数据时得到的结果: ,

正如您所看到的,xhr的头已经损坏。这将导致文件上载完全失败。以下是我使用的代码:

function generateBoundary()
{
    var chars = '0123456789',
        out   = '';

    for( var i = 0, len = chars.length; i < 30; i++) {
       out += chars[Math.floor(Math.random()*len)];
    }

    return '----' + out;
}

function getMultipartFd(file, boundary)
{
    var rn   = '\r\n',
        body = '';

    body  = boundary + rn;
    body += 'Content-Disposition: form-data; name="Files[]"; filename="' + file.name + '"' + rn;
    body += 'Content-Type: ' + file.type + rn + rn;
    body += file.getAsBinary() + rn;

    return body;
}

$(function(){

    $startUpload.click(function(){
        var url      = $uploadForm.attr('action'),
            xhr      = new XMLHttpRequest(),
            boundary = generateBoundary(),
            file     = null,
            body     = '';

        file = $SOME_ELEMENT_WITH_ATTACHED_FILE.file;
        body = getMultipartFd(file, boundary);

console.info(file);
console.info(body);

        xhr.upload.onload = function(){
            console.info('done');
        };

        xhr.open('POST', url, true);
        xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
        xhr.sendAsBinary(body + boundary + '--' + '\r\n');
        return false;
    });

});
这里还有文件和正文变量的转储: ,


有人知道为什么xhr会以这种方式破坏标题吗?

我是在寻找问题的范围。我尝试在WinXP下的全新Firefox安装中使用代码,我的主要系统是Arch Linux。问题依然存在。我发现它有一个额外的属性叫做“multipart”。将该值设置为true后,标头就可以了,但不会触发my xhr.events-发送文件后JS崩溃

我使用Firebug的JS调试器进行了更深入的研究,发现在xhr.multipart=true之后;代码跳入jQuery库的深水中,在这里,围绕一些奇怪的事件发生了奇怪的事情

更奇怪的是,Firebug控制台中的标题/内容似乎正确,但在HttpFox扩展中,它已损坏