Note that there are some explanatory texts on larger screens.

plurals
  1. POhow do I access XHR responseBody (for binary data) from Javascript in IE?
    primarykey
    data
    text
    <p>I've got a web page that uses <a href="http://msdn.microsoft.com/en-us/library/ms535874(VS.85).aspx" rel="noreferrer">XMLHttpRequest</a> to download a binary resource. </p> <p>In Firefox and Gecko I can use responseText to get the bytes, even if the bytestream includes binary zeroes. I may need to coerce the mimetype with <code>overrideMimeType()</code> to make that happen. In IE, though, responseText doesn't work, because it appears to terminate at the first zero. If you read 100,000 bytes, and byte 7 is a binary zero, you will be able to access only 7 bytes. IE's XMLHttpRequest exposes a <code>responseBody</code> property to access the bytes. I've seen a few posts suggesting that it's impossible to access this property in any meaningful way directly from Javascript. This sounds crazy to me. </p> <p><code>xhr.responseBody</code> <em>is</em> accessible from VBScript, so the obvious workaround is to define a method in VBScript in the webpage, and then call that method from Javascript. See <a href="http://code.google.com/p/jsdap/source/browse/trunk/api.js?r=64" rel="noreferrer">jsdap</a> for one example. <strong>EDIT: DO NOT USE THIS VBScript!!</strong></p> <pre><code>var IE_HACK = (/msie/i.test(navigator.userAgent) &amp;&amp; !/opera/i.test(navigator.userAgent)); // no no no! Don't do this! if (IE_HACK) document.write('&lt;script type="text/vbscript"&gt;\n\ Function BinaryToArray(Binary)\n\ Dim i\n\ ReDim byteArray(LenB(Binary))\n\ For i = 1 To LenB(Binary)\n\ byteArray(i-1) = AscB(MidB(Binary, i, 1))\n\ Next\n\ BinaryToArray = byteArray\n\ End Function\n\ &lt;/script&gt;'); var xml = (window.XMLHttpRequest) ? new XMLHttpRequest() // Mozilla/Safari/IE7+ : (window.ActiveXObject) ? new ActiveXObject("MSXML2.XMLHTTP") // IE6 : null; // Commodore 64? xml.open("GET", url, true); if (xml.overrideMimeType) { xml.overrideMimeType('text/plain; charset=x-user-defined'); } else { xml.setRequestHeader('Accept-Charset', 'x-user-defined'); } xml.onreadystatechange = function() { if (xml.readyState == 4) { if (!binary) { callback(xml.responseText); } else if (IE_HACK) { // call a VBScript method to copy every single byte callback(BinaryToArray(xml.responseBody).toArray()); } else { callback(getBuffer(xml.responseText)); } } }; xml.send(''); </code></pre> <p>Is this really true? The best way? copying every byte? For a large binary stream that's not going to be very efficient. </p> <p>There is also a <em>possible</em> technique using ADODB.Stream, which is a COM equivalent of a MemoryStream. <a href="http://help.dottoro.com/ljgqlcox.php" rel="noreferrer">See here</a> for an example. It does not require VBScript but does require a separate COM object.</p> <pre><code>if (typeof (ActiveXObject) != "undefined" &amp;&amp; typeof (httpRequest.responseBody) != "undefined") { // Convert httpRequest.responseBody byte stream to shift_jis encoded string var stream = new ActiveXObject("ADODB.Stream"); stream.Type = 1; // adTypeBinary stream.Open (); stream.Write (httpRequest.responseBody); stream.Position = 0; stream.Type = 1; // adTypeBinary; stream.Read.... /// ???? what here } </code></pre> <p>But that's not going to work well - ADODB.Stream is disabled on most machines these days. </p> <hr> <p>In The IE8 developer tools - the IE equivalent of Firebug - I can see the responseBody is an array of bytes and I can even see the bytes themselves. The data is <em>right there</em>. I don't understand why I can't get to it. </p> <p>Is it possible for me to read it with responseText? </p> <p>hints? (other than defining a VBScript method)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload