Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's how I did it. </p> <p>This technique is provided in an answer to another SO question, but it's also relevant here. </p> <p>I didn't want to base64 encode anything. I wanted to download and parse binary files in the browser via Javascript, without modifying the server to encode them specially. I found that in Firefox, by coercing the mimetype of the response via <code>overrideMimeType()</code>, I could use <code>XMLHttpRequest.responseText</code>. On IE, it's different because:</p> <ul> <li><p><code>responseText</code> on IE truncates at the first zero. For binary streams this is a big problem.</p></li> <li><p>there is no <code>XMLHttpRequest.overrideMimeType()</code>, to force IE to treat binary streams as text.</p></li> <li><p>while there is a <code>XMLHttpRequest.responseBody</code> (IE only!) that is specifically designed to be used with binary data streams, maddeningly that property is not usable from Javascript. </p></li> </ul> <p>Therefore, the need is to convert IE's <code>responseBody</code> property into a thing that looks like <code>responseText</code> from FireFox, with the mime-type coercion. This is possible using injected VBScript. </p> <p>To make it cross-browser, you need to just pack up the browser-specific logic in a conditional. This is what I used:</p> <pre><code>// one-time code if(/msie/i.test(navigator.userAgent) &amp;&amp; !/opera/i.test(navigator.userAgent)) { var IEBinaryToArray_ByteStr_Script = "&lt;!-- IEBinaryToArray_ByteStr --&gt;\r\n"+ "&lt;script type='text/vbscript'&gt;\r\n"+ "Function IEBinaryToArray_ByteStr(Binary)\r\n"+ " IEBinaryToArray_ByteStr = CStr(Binary)\r\n"+ "End Function\r\n"+ "Function IEBinaryToArray_ByteStr_Last(Binary)\r\n"+ " Dim lastIndex\r\n"+ " lastIndex = LenB(Binary)\r\n"+ " if lastIndex mod 2 Then\r\n"+ " IEBinaryToArray_ByteStr_Last = Chr( AscB( MidB( Binary, lastIndex, 1 ) ) )\r\n"+ " Else\r\n"+ " IEBinaryToArray_ByteStr_Last = "+'""'+"\r\n"+ " End If\r\n"+ "End Function\r\n"+ "&lt;/script&gt;\r\n"; // inject VBScript document.write(IEBinaryToArray_ByteStr_Script); } // each time you make a request for a binary resource: var req = (function() { if (window.XMLHttpRequest) { return new window.XMLHttpRequest(); } else { try { return new ActiveXObject("MSXML2.XMLHTTP"); } catch(ex) { return null; } } })(); var fileContents = ""; var filesize = -1; var readByteAt = function(i){ return fileContents.charCodeAt(i) &amp; 0xff; }; req.open("GET", url, true); if(/msie/i.test(navigator.userAgent) &amp;&amp; !/opera/i.test(navigator.userAgent)) { // IE-specific logic here // helper to convert from responseBody to a "responseText" like thing var convertResponseBodyToText = function (binary) { var byteMapping = {}; for ( var i = 0; i &lt; 256; i++ ) { for ( var j = 0; j &lt; 256; j++ ) { byteMapping[ String.fromCharCode( i + j * 256 ) ] = String.fromCharCode(i) + String.fromCharCode(j); } } var rawBytes = IEBinaryToArray_ByteStr(binary); var lastChr = IEBinaryToArray_ByteStr_Last(binary); return rawBytes.replace(/[\s\S]/g, function( match ) { return byteMapping[match]; }) + lastChr; }; req.setRequestHeader("Accept-Charset", "x-user-defined"); req.onreadystatechange = function(event){ if (req.readyState == 4) { if (req.status == 200) { fileContents = convertResponseBodyToText(req.responseBody); fileSize = fileContents.length-1; // invoke a callback here, if you like... } else{ alert("download failed, status " + req.status); } } }; req.send(); } else { // ff/Gecko/Webkit specific stuff here req.onreadystatechange = function(aEvt) { if (req.readyState == 4) { // completed if(req.status == 200){ // status == OK fileContents = binStream.req.responseText; filesize = fileContents.length; // invoke a callback here, if you like... } else { alert("download failed, status " + req.status); } } }; // coerce response type req.overrideMimeType('text/plain; charset=x-user-defined'); req.send(null); } </code></pre> <p>...then call <code>readByte(i)</code> to get the byte at the <em>ith</em> position in the binary file.</p> <p>Good luck.</p> <p><a href="http://miskun.com/javascript/internet-explorer-and-binary-files-data-access/">Credit to Miskun</a> for the VBScript conversion logic.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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