5

Is Firefox the only that supports the sendAsBinary method?

5 Answers 5

3

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

2
  • 2
    I believe the workaround is to define you own sendAsBinary via prototype as such:try { if (XMLHttpRequest.prototype.sendAsBinary) return; XMLHttpRequest.prototype.sendAsBinary = function(datastr) { function byteValue(x) { return x.charCodeAt(0) & 0xff; } var ords = Array.prototype.map.call(datastr, byteValue); var ui8a = new Uint8Array(ords); this.send(ui8a.buffer); } } catch (e) {} Commented Mar 30, 2012 at 1:58
  • 11
    I like how Marko answered Mr. Polo.
    – Jimmery
    Commented Sep 20, 2012 at 14:32
2

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

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

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.

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.

1

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.

1

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

//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) {}
0

The workaround for Chrome is explained at the following URL:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.