0

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!

3
  • which line exactly does the error point to? Commented Nov 2, 2019 at 17:28
  • The error points to zip.js, line no. 920 where the default error handling is used when using the createReader function Commented Nov 2, 2019 at 17:36
  • createReader : function(reader, callback, onerror) { onerror = onerror || onerror_default; Commented Nov 2, 2019 at 17:36

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

12 Comments

I treid that @nada, i am still getting the same error.
@arnavJJ have you found a solution?
nope, I am stuck. No matter what I do , I get the same error. I even tried making a File object of the response and passing it as a parameter to another function, but I am getting the same error.
@arnavJJ ok, without seeing the actual code it is not easy for me understand how you are doing all of it so lets first try by by commenting out ALL of the code you posted in the question and just try this.Emit("dataReady", { data : "test"}); does it give what you expected?
@arnavJJ I understand. Ok, let's try the whole thing again now without all the binds. That is, remove all the binds from the code and do like I suggested in the answer, put var _root = this; outside the onload and swap _root for all the "this"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.