2015-06-25 77 views
3
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); 
} 

我想誇大上傳文件,但該功能提供了一個錯誤:未捕獲的使用不正確頭檢查pako.js

Uncaught incorrect header check

+0

非常感謝非常龐 –

回答

0

我能夠通過使用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"); 
    } 
}