通过ajax下载zip文件(包含xml和图像),并在IE中的localstorage中存储二进制数据

时间:2013-05-09 07:20:36

标签: ajax binary

我能够通过ajax调用从服务器下载zip文件(2mb)并将二进制数据存储在Web存储中,因为这在IE中不起作用(7,8,9)。以下是我的代码。

    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);
        }
    }

在VB脚本中将二进制数据转换为数组。以下是代码。

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

将此缓冲区值存储在html5 Web存储中。

我没有在IE浏览器中获得完整的内容,它在FF,Chrome中工作。

任何帮助!

1 个答案:

答案 0 :(得分:0)