I am trying to send binary data to a server. This works exactly the way I need it in Chrome 15:
XMLHttpRequest.prototype.sendAsBinary = function(datastr) { function byteValue(x) { return x.charCodeAt(0) & 0xff; } var ords = Array.prototype.map.call(datastr, byteValue); var ui8a = new Int8Array(ords); this.send(ui8a.buffer); };
However, I need this to work in some browsers that do not support the Int8Array (or Blobs) type.
Perhaps the solution is to create my own implementation of ArrayBuffer (ui8a.buffer - ArrayBuffer). The problem is that I do not know what this object is; checking it in the JavaScript console shows only the byteLength property.
Refresh . I feel like approaching, but my transformation is wrong. Here is what I am trying:
XMLHttpRequest.prototype.sendAsBinary = function(datastr) { var blob; function byteValue(x) { return x.charCodeAt(0) & 0xff; } var ords = Array.prototype.map.call(datastr, byteValue); if (window.Int8Array) { var ui8a = new Int8Array(ords); blob = ui8a.buffer; } else { var strArray = Array.prototype.map.call(ords, function(x) { if (x > 127) x = x-256; return String.fromCharCode(x); }); blob = strArray.join(); } this.send(blob); };
My blob ends up twice as long as it should be - this alludes to your mention of encoding two bytes at a time. But I'm not quite sure how to do this (for now) ...
source share