1

I was able to download the zip file (2mb) from server through ajax call and storing binary data in the web storage where as this is not working in IE (7,8,9). Below is my code.

    var req = new XMLHttpRequest();
    req.open('GET', filename, false);
    req.onreadystatechange=function()
    {
        if (req.readyState == 4)
        {
        if(req.status!=200){
            chk_file = false;
            alert('Sorry, Error occured while downloading the question paper. HTTP Error Code: '+req.status);
        }
        }
    }
    if (req.overrideMimeType) {
        req.overrideMimeType('text/plain; charset=x-user-defined');
    } else {
        req.setRequestHeader('Accept-Charset', 'x-user-defined');
    }
    req.send('hello');

    var buffer = "";
    var dbata;
    try {
        bdata = BinaryToArray(req.responseBody).toArray();
        for (var i = 0, len = bdata.length - 1; i < len; i++) {// dbata is one byte too long. Why ???
            buffer += String.fromCharCode(bdata[i] & 0xFF);
        }
    } catch(e) {
        bdata = req.responseText;
        for (var i = 0, len = bdata.length; i < len; i++) {
            buffer += String.fromCharCode(bdata.charCodeAt(i) & 0xFF);
        }
    }

Converting Binary data to array in VB script . following is the code.

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

    if (IE_HACK) {
        var vbScript = '<scr' + 'ipt type="text/vbscript">\n'+
            '<!-' + '-\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'+
            '--' + '>\n' +
            '</scr' + 'ipt>';

        //$(vbScript).insertAfter("script:last");
        document.write(vbScript);

Storing this buffer value in html5 web storage.

I am not getting complete content in IE where it is working in FF, Chrome.

Any help!

user2024285
  • 151
  • 5
  • How did it go? I am trying to do something similar, I do an AJAX request with jQuery, in the response I can either get the data for a CSV file or data for a ZIP file. I am using https://github.com/eligrey/FileSaver.js to 'build' the files from the data response. And although a ZIP file get generated it doesn't work, it's something about being corrupt. I see that you are fixing this by overriding the MIME type with charset=x-user-defined as in http://stuk.github.io/jszip/ but I haven't being successful with this with jQuery. Any clues? – orlybg Apr 05 '14 at 01:16

1 Answers1

0

Maybe this can help you, but guess you solved this by now, but it might help people that sees this question

is there a cross-browser alternative to the xhr.overrideMimeType() function?

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data

https://github.com/eligrey/FileSaver.js

http://stuk.github.io/jszip/

Community
  • 1
  • 1
orlybg
  • 599
  • 1
  • 5
  • 15