Chrome XMLHttpRequest POST 编码问题
我使用 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 中它工作得很好。看一下 firebug 屏幕截图: http://s8.postimage.org/4dw4gd5j7/Untitled.png
然而在 Chrome 中它看起来很糟糕:http://s8.postimage.org/h7yrng8cl/chrome.png
看起来像是编码问题。我尝试添加charset
utf-8,但仍然不起作用。
I send file with XMLHttpRequest, like this:
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);
}
In Firefox it works perfectly. Take a look a the firebug screenshot: http://s8.postimage.org/4dw4gd5j7/Untitled.png
However in Chrome it looks terrible: http://s8.postimage.org/h7yrng8cl/chrome.png
Looks like an encoding issue. I tried to add a charset
utf-8, but it still didn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不同浏览器处理不同。你会尝试 jquery $.ajax 吗?像 jquery、YUI 这样的 javascript 库应该可以消除浏览器的差异。
different browser handle different. would you try jquery $.ajax . javascript library like jquery, YUI is supposed to iron browser difference.