【问题标题】:Uncaught incorrect header check using pako.js使用 pako.js 未捕获不正确的标头检查
【发布时间】:2015-06-25 02:44:09
【问题描述】:
function loadFileAsBinary()
{
    var fileToLoad = document.getElementById("fileToLoad").files[0];

    var fileReader = new FileReader();

    fileReader.onload = function(fileLoadedEvent) 
    {
        var textFromFileLoaded = fileLoadedEvent.target.result;

        var rs       = textFromFileLoaded;

        var charData = rs.split('').map(function(x){return x.charCodeAt(0);}); 
        console.log(charData);

        var bindata  = new Uint8Array(charData);
        console.log(bindata);

        var plain    = pako.inflate(bindata, {to: 'string' });

        var strData  = String.fromCharCode.apply(null, new Uint16Array(plain));

        document.getElementById("inputTextToSave").value = strData;

    };

    fileReader.readAsBinaryString(fileToLoad);
}

我想给上传文件充气,但是这个函数报错:

未捕获的错误标头检查

【问题讨论】:

  • 非常感谢庞先生

标签: javascript zlib


【解决方案1】:

我能够通过使用r.readAsArrayBuffer(f);pako.inflate(new Uint8Array( e.target.result ) , {"to":"string"})

这是我的代码:

function changeInputFile(evt){
    // Retrieve the first (and only!) File from the FileList object
    var f = evt.target.files[0];
    if (f) {
        let r = new FileReader();
        r.onload = function(e) {
            var contents = e.target.result;
            console.debug( "User layout file:\n"
                         + "name: " + f.name + "\n"
                         + "type: " + f.type + "\n"
                         + "size: " + f.size + " bytes\n"
                         )
            );
            try {
                let jsonContent = null;
                if ( f.type == "application/gzip" ) {
                    jsonContent=pako.inflate(new Uint8Array( e.target.result ) , {"to":"string"});
                } else {
                   // ...
                }
                // ...
            } catch(e) {
                console.error(e)
            }
        }
        r.readAsArrayBuffer(f); 
    } else {
        console.error("Failed to load file");
    }
}

【讨论】:

    猜你喜欢
    • 2018-12-13
    • 2021-08-31
    • 2015-09-21
    • 2013-03-06
    • 2015-05-29
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 2013-10-26
    相关资源
    最近更新 更多