0

我使用 XMLHttpRequest 发送文件,如下所示:

self.xhr.open("POST", params.url + "?al=" + params.accessLevel + "&desc=" + params.desc + "&album_id=" + params.aid);

var boundary = "xxxxxxxxx";
self.xhr.setRequestHeader("Content-Type", "multipart/form-data, boundary="+boundary);
self.xhr.setRequestHeader("Cache-Control", "no-cache");
self.xhr.setRequestHeader("X-Requested-With", "ajx");

var body = "--" + boundary + "\r\n";
body += "Content-Disposition: form-data; name='"+(params.fieldName || 'file')+"'; filename='" + encodeURIComponent(params.file.name) + "'\r\n";
body += "Content-Type: application/octet-stream\r\n\r\n";
body += self.reader.result + "\r\n";
body += "--" + boundary + "--";

if(self.xhr.sendAsBinary) {
    // firefox
    //self.xhr.overrideMimeType("application/octet-stream; charset=utf-8");
    self.xhr.sendAsBinary(body);
} else {
    // chrome (W3C spec.)
    self.xhr.send(body);
}

在 Firefox 中它可以完美运行。看看萤火虫截图:http ://s8.postimage.org/4dw4gd5j7/Untitled.png

但是在 Chrome 中它看起来很糟糕:http ://s8.postimage.org/h7yrng8cl/chrome.png

看起来像一个编码问题。我试图添加一个charsetutf-8,但它仍然没有工作。

4

1 回答 1

0

不同的浏览器处理不同。你会尝试 jquery $.ajax 吗?像 jquery、YUI 这样的 javascript 库应该可以消除浏览器的差异。

于 2011-11-15T22:42:32.670 回答