简体   繁体   中英

zip.js : File format is not recognized, javascript

I am getting a zip file response from a HTTP GET request which contains two other files in it of extensions.log and.out. I am using zip.js to successfully read the data from the.log file but when I try to pass the text data, read from the.log file, as an argument to an event, I get the error:

"File Format is not recognized"

I am doing this on client-side javascript.

Here is my code:

var xhr = new XMLHttpRequest();
xhr.onload = function(e) {
    var blobData = new Blob([this.response],{type : "application/zip"});
    zip.createReader(new zip.BlobReader(blobData), function(zipReader){
        zipReader.getEntries(function(entries){

                entries[1].getData(new zip.TextWriter(), function(text){
                        console.log(text);

                        this.Emit("dataReady", {
                            data : text});
                });
        }.bind(this));
    }.bind(this),this.onerror);
}.bind(this);

 xhr.open("GET","path/to/url/file.zip",true);
 xhr.setRequestHeader("Content-type","application/zip");
 xhr.responseType = 'blob';    
 xhr.send();

The error I am getting is:

File format is not recognized.

Please advice as I am using zip.js and reading a zip file response from an http request for the first time.Thanks!

Can you verify that you are using the correct this at this line

this.Emit("dataReady", { data : text});

I am not sure to where you are emitting the text but you may be calling Emit on the wrong object. If this is the case then please take a look at this link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call and do something like to quickly test. Note, I notice that you are already binding "this"

var xhr = new XMLHttpRequest();
var _root = this;
xhr.onload = function(e) {
    var blobData = new Blob([this.response],{type : "application/zip"});
    zip.createReader(new zip.BlobReader(blobData), function(zipReader){
        zipReader.getEntries(function(entries){

                entries[1].getData(new zip.TextWriter(), function(text){
                        console.log(text);

                        _root.Emit("dataReady", {
                            data : text});
                });
        }.bind(this));
    }.bind(this),this.onerror);
}.bind(this);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM