美文网首页前端开发小册
js 获取文件byte数组并解压

js 获取文件byte数组并解压

作者: 国服最坑开发 | 来源:发表于2020-01-14 17:24 被阅读0次

用到的解压库:https://github.com/nodeca/pako

        var xhr = new XMLHttpRequest();
        xhr.open('GET', "http://somewhere/sometime.zip");
        xhr.responseType = 'arraybuffer';
        xhr.onreadystatechange = function getPdfOnreadystatechange(e) {
            console.log("xhr.readyState:" + xhr.readyState + "   xhr.status:" + xhr.status);
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
                        xhr.responseArrayBuffer || xhr.response);
                    console.log(data);

                    let unzip = new pako.Inflate();
                    unzip.push(data);
                    let out = unzip.result;
                    console.log(out.length);

                } else {
                    console.log("error");
                }
            }
        };
        xhr.send(null);

相关文章

网友评论

    本文标题:js 获取文件byte数组并解压

    本文链接:https://www.haomeiwen.com/subject/biuaactx.html