Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 502411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:17:19+00:00 2026-05-13T06:17:19+00:00

I’ve got a web page that uses XMLHttpRequest to download a binary resource. In

  • 0

I’ve got a web page that uses XMLHttpRequest to download a binary resource.

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 overrideMimeType() 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 responseBody 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.

xhr.responseBody is 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 jsdap for one example. EDIT: DO NOT USE THIS VBScript!!

var IE_HACK = (/msie/i.test(navigator.userAgent) && 
               !/opera/i.test(navigator.userAgent));   

// no no no!  Don't do this! 
if (IE_HACK) document.write('<script type="text/vbscript">\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\
</script>'); 

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('');

Is this really true? The best way? copying every byte? For a large binary stream that’s not going to be very efficient.

There is also a possible technique using ADODB.Stream, which is a COM equivalent of a MemoryStream. See here for an example. It does not require VBScript but does require a separate COM object.

if (typeof (ActiveXObject) != "undefined" && 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
}

But that’s not going to work well – ADODB.Stream is disabled on most machines these days.


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 right there. I don’t understand why I can’t get to it.

Is it possible for me to read it with responseText?

hints? (other than defining a VBScript method)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:17:19+00:00Added an answer on May 13, 2026 at 6:17 am

    Yes, the answer I came up with for reading binary data via XHR in IE, is to use VBScript injection. This was distasteful to me at first, but, I look at it as just one more browser dependent bit of code.
    (The regular XHR and responseText works fine in other browsers; you may have to coerce the mime type with XMLHttpRequest.overrideMimeType(). This isn’t available on IE).

    This is how I got a thing that works like responseText in IE, even for binary data.
    First, inject some VBScript as a one-time thing, like this:

    if(/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) {
        var IEBinaryToArray_ByteStr_Script =
        "<!-- IEBinaryToArray_ByteStr -->\r\n"+
        "<script type='text/vbscript' language='VBScript'>\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"+
        "</script>\r\n";
    
        // inject VBScript
        document.write(IEBinaryToArray_ByteStr_Script);
    }
    

    The JS class I’m using that reads binary files exposes a single interesting method, readCharAt(i), which reads the character (a byte, really) at the i’th index. This is how I set it up:

    // see doc on http://msdn.microsoft.com/en-us/library/ms535874(VS.85).aspx
    function getXMLHttpRequest() 
    {
        if (window.XMLHttpRequest) {
            return new window.XMLHttpRequest;
        }
        else {
            try {
                return new ActiveXObject("MSXML2.XMLHTTP"); 
            }
            catch(ex) {
                return null;
            }
        }
    }
    
    // this fn is invoked if IE
    function IeBinFileReaderImpl(fileURL){
        this.req = getXMLHttpRequest();
        this.req.open("GET", fileURL, true);
        this.req.setRequestHeader("Accept-Charset", "x-user-defined");
        // my helper to convert from responseBody to a "responseText" like thing
        var convertResponseBodyToText = function (binary) {
            var byteMapping = {};
            for ( var i = 0; i < 256; i++ ) {
                for ( var j = 0; j < 256; j++ ) {
                    byteMapping[ String.fromCharCode( i + j * 256 ) ] =
                        String.fromCharCode(i) + String.fromCharCode(j);
                }
            }
            // call into VBScript utility fns
            var rawBytes = IEBinaryToArray_ByteStr(binary);
            var lastChr = IEBinaryToArray_ByteStr_Last(binary);
            return rawBytes.replace(/[\s\S]/g,
                                    function( match ) { return byteMapping[match]; }) + lastChr;
        };
    
        this.req.onreadystatechange = function(event){
            if (that.req.readyState == 4) {
                that.status = "Status: " + that.req.status;
                //that.httpStatus = that.req.status;
                if (that.req.status == 200) {
                    // this doesn't work
                    //fileContents = that.req.responseBody.toArray(); 
    
                    // this doesn't work
                    //fileContents = new VBArray(that.req.responseBody).toArray(); 
    
                    // this works...
                    var fileContents = convertResponseBodyToText(that.req.responseBody);
    
                    fileSize = fileContents.length-1;
                    if(that.fileSize < 0) throwException(_exception.FileLoadFailed);
                    that.readByteAt = function(i){
                        return fileContents.charCodeAt(i) & 0xff;
                    };
                }
                if (typeof callback == "function"){ callback(that);}
            }
        };
        this.req.send();
    }
    
    // this fn is invoked if non IE
    function NormalBinFileReaderImpl(fileURL){
        this.req = new XMLHttpRequest();
        this.req.open('GET', fileURL, true);
        this.req.onreadystatechange = function(aEvt) {
            if (that.req.readyState == 4) {
                if(that.req.status == 200){
                    var fileContents = that.req.responseText;
                    fileSize = fileContents.length;
    
                    that.readByteAt = function(i){
                        return fileContents.charCodeAt(i) & 0xff;
                    }
                    if (typeof callback == "function"){ callback(that);}
                }
                else
                    throwException(_exception.FileLoadFailed);
            }
        };
        //XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com] 
        this.req.overrideMimeType('text/plain; charset=x-user-defined');
        this.req.send(null);
    }
    

    The conversion code was provided by Miskun.

    Very fast, works great.

    I used this method to read and extract zip files from Javascript, and also in a class that reads and displays EPUB files in Javascript. Very reasonable performance. About half a second for a 500kb file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 279k
  • Answers 279k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to override Equals for your object. Assert uses… May 13, 2026 at 3:30 pm
  • Editorial Team
    Editorial Team added an answer one of the easiest ways is with audio queues. its… May 13, 2026 at 3:30 pm
  • Editorial Team
    Editorial Team added an answer Each user has their own blogs, and you can create… May 13, 2026 at 3:30 pm

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.