Javascript 传出数据的XmlHttpRequest重写EMIMetype
我希望Javascript 传出数据的XmlHttpRequest重写EMIMetype,javascript,ajax,xmlhttprequest,Javascript,Ajax,Xmlhttprequest,我希望XmlHttpRequest不接触(即转换为UTF-8或任何其他编码)它向服务器发送和从服务器接收的任何字符 对于从服务器接收到的字符,我知道我可以执行XmlHttpRequest#overrideMimeType('text/plain;charset=x-user-defined'),而字符则不受影响 但是,对于通过XmlHttpRequest#send调用发送到服务器的字符,我找不到方法告诉XmlHttpRequest不要触摸这些字符。我看到XmlHttpRequest将它们编码到U
XmlHttpRequest
不接触(即转换为UTF-8
或任何其他编码)它向服务器发送和从服务器接收的任何字符
对于从服务器接收到的字符,我知道我可以执行XmlHttpRequest#overrideMimeType('text/plain;charset=x-user-defined')
,而字符则不受影响
但是,对于通过
XmlHttpRequest#send
调用发送到服务器的字符,我找不到方法告诉XmlHttpRequest
不要触摸这些字符。我看到XmlHttpRequest
将它们编码到UTF-8
中,无论我做了什么尝试。有没有办法防止这种情况发生?您可以尝试对在base64中发送的数据进行编码。据我所知,无论采用何种编码(UTF-8/Latin-1/whatever),这都是有效的。我在上传从麦克风抓取的音频(通过闪存)时遇到了类似的问题。
下面是我如何解决它的(在FF 9和Crimum 15中进行了测试)。下面的js代码将二进制输入作为字符串,并将其上载到服务器,而不进行任何更改/编码。
希望这对某人有用
var audioBytes = <the data you want sent untouched>
var crlf = '\r\n';
var body = '';
var doubleDash = '--';
var boundary = '12345678901234567890';
var file = {
name: "online-recording.wav",
type: "audio/x-wav",
size: audioBytes.length,
recordedContent: audioBytes
};
var body = doubleDash + boundary + crlf +
'Content-Disposition: form-data; name="file"; ' +
'filename="' + unescape(encodeURIComponent(file.name)) + '"' + crlf +
'Content-Type: ' + file.type + crlf + crlf +
file.recordedContent + crlf +
doubleDash + boundary + doubleDash + crlf;
// copy binary content into a ByteArray structure, so that the browser doesn't mess up encoding
var audioByteArray = new Uint8Array(body.length);
for (var i=0; i< audioByteArray.length; i++) {
audioByteArray[i] = body.charCodeAt(i);
}
// use an XMLHttpRequest to upload the file
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
console.log(xhr.readyState);
};
xhr.open('post', url, true);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
// setup AJAX event handlers here
xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
xhr.send(audioByteArray.buffer);
var audioBytes=
var crlf='\r\n';
变量体=“”;
var doubleDash='--';
变量边界='123456789001234567890';
变量文件={
名称:“在线录制.wav”,
类型:“音频/x-wav”,
大小:audioBytes.length,
录制内容:音频字节
};
变量主体=双破折号+边界+crlf+
'内容配置:表单数据;name=“file”;'+
'filename=“”+unescape(encodeURIComponent(file.name))+“”+crlf+
'内容类型:'+file.Type+crlf+crlf+
file.recordedContent+crlf+
双破折号+边界+双破折号+crlf;
//将二进制内容复制到ByteArray结构中,这样浏览器就不会弄乱编码
var audioByteArray=新的UINT8阵列(体长);
对于(var i=0;i
诀窍是从ArrayBuffer构建请求,它被视为二进制数据,因此浏览器不会弄乱它。或者,如果您只针对firefox,您可以使用xhr.sendAsBinary()(据我所知,它没有移植到任何其他浏览器,而上面的代码似乎与W3C兼容)是的,但是我需要在服务器上解码base64,这样就无法达到目的。我希望将字符按原样发送到服务器。是的,但据我所知,这是确保服务器接收的所有内容都是客户端发送的内容的最佳方法。还有可能只是UTF-8解码你的字符串:)我想你应该要回原稿。你知道吗?我被难住了。+1,这是在中描述的,在FF9中显然是新的。