【发布时间】:2011-11-15 20:00:17
【问题描述】:
我用 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
看起来像是编码问题。我尝试添加一个charset utf-8,但还是不行。
【问题讨论】:
-
您的问题是数据设置不正确还是调试器文本混乱?后者看起来是浏览器错误。
标签: google-chrome xmlhttprequest fileapi