javascript XMLHttpRequest:浏览器支持 sendAsBinary?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4236153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:45:20  来源:igfitidea点击:

XMLHttpRequest: Browser support for sendAsBinary?

javascriptajax

提问by Alex Polo

Is Firefox the only that supports the sendAsBinary method?

Firefox 是唯一支持 sendAsBinary 方法的吗?

采纳答案by Marko

At the moment, I believe only FF3+ supports this, though there is a workaroundfor Chrome.

目前,我只相信FF3 +支持这一点,虽然是有解决办法

回答by jamshid

The links around http://code.google.com/p/chromium/issues/detail?id=35705are very confusing, but I do not think there is any workaround on Chrome 8 for POST'ing binary data.

http://code.google.com/p/chromium/issues/detail?id=35705周围的链接非常混乱,但我认为 Chrome 8 上没有任何用于 POST 二进制数据的解决方法。

You can convert the data to base64 and upload that, but then the server has to be able to decode it.

您可以将数据转换为 base64 并上传,但服务器必须能够对其进行解码。

Chrome 9 (currently in Dev channel, not even Beta yet) lets you do XmlHttpRequest.send(blob) where the blob's bytes are sent as-is (not converted to utf-8), so the non-standard XmlHttpRequest.sendAsBinary() is not necessary for binary file uploads.

Chrome 9(目前在 Dev 频道中,甚至还没有 Beta 版)允许您执行 XmlHttpRequest.send(blob),其中 blob 的字节按原样发送(未转换为 utf-8),因此非标准 XmlHttpRequest.sendAsBinary()二进制文件上传不需要。

You must create this blob from the "binary" string that is in evt.target.result after a successful FileReader.readAsBinaryString(). That requires using ArrayBuffer and Uint8Array, which are not available in Chrome 8.

在 FileReader.readAsBinaryString() 成功后,您必须从 evt.target.result 中的“二进制”字符串创建此 blob。这需要使用 ArrayBuffer 和 Uint8Array,它们在 Chrome 8 中不可用。

回答by casablanca

As far as I know, yes, only Firefox supports it. It's not part of the W3C standard, so there's no guarantee that it'll ever be supported by any other browser.

据我所知,是的,只有 Firefox 支持它。它不是W3C 标准的一部分,因此不能保证它会被任何其他浏览器支持。

回答by llamerr

I had same error, but I'm also using Prototype.js. Seems it has some replacement for map function and it were throwing TypeError for me Object ..file data here.. has no method 'each'So i used this replacement instead

我有同样的错误,但我也在使用 Prototype.js。似乎它有一些 map 函数的替代品,它为我抛出了 TypeErrorObject ..file data here.. has no method 'each'所以我改用了这个替代品

//fix sendAsBinary for chrome
try {
  if (typeof XMLHttpRequest.prototype.sendAsBinary == 'undefined') {
    XMLHttpRequest.prototype.sendAsBinary = function(text){
      var data = new ArrayBuffer(text.length);
      var ui8a = new Uint8Array(data, 0);
      for (var i = 0; i < text.length; i++) ui8a[i] = (text.charCodeAt(i) & 0xff);
      this.send(ui8a);
    }
  }
} catch (e) {}

回答by Поломошнов Александр

The workaround for Chrome is explained at the following URL:

以下 URL 解释了 Chrome 的解决方法:

http://code.google.com/p/chromium/issues/detail?id=35705

http://code.google.com/p/chromium/issues/detail?id=35705