Is Firefox the only that supports the sendAsBinary method?
5 Answers
At the moment, I believe only FF3+ supports this, though there is a workaround for Chrome.
-
2I 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
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.
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.
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) {}
The workaround for Chrome is explained at the following URL: