Stuk / jszip

Create, read and edit .zip files with Javascript
https://stuk.github.io/jszip/
Other
9.74k stars 1.3k forks source link

Uncaught (in promise) TypeError: zip.generateAsync is not a function(…) #347

Open cici004 opened 8 years ago

cici004 commented 8 years ago

Hi, I have an issue when I run my zip download button.

It pops the error as below:

Uncaught (in promise) TypeError: zip.generateAsync is not a function(…) js/gis/dijit/Download.js:223

I also put my syntax file in the following link

https://github.com/cici004/SMILE-Test

Anybody knows why? Thanks

dduponchel commented 8 years ago

Are you sure you get the 3.1.2 version of JSZip ? What shows a console.log(JSZip.version) ? Your test repository lacks an index.html file (and possibly the package.json/bower.json/whatever file for dependencies) for me to test it.

Regarding your request function, you tries to download a binary file as text (the default behavior of xhr requests). You should add httpRequest.responseType = "arraybuffer"; and use this.response instead of this.responseText.

cici004 commented 8 years ago

Thanks for your reply David! When I tested console.log(JSZip.version), it returned "undefined". I am trying to batch download a group of PDF files for one action. When you mentioned httpRequest.responseType = "arraybuffer"; and this.response, where can I find/add them, I did not find places to replace them in jszip.js. I uploaded my index.html(named"smile") to Github in the same link. Could you have a look at it?

Thanks, Xi

On Sat, Aug 27, 2016 at 1:53 AM, David Duponchel notifications@github.com wrote:

Are you sure you get the 3.1.2 version of JSZip ? What shows a console.log(JSZip.version) ? Your test repository lacks an index.html file (and possibly the package.json/bower.json/whatever file for dependencies) for me to test it.

Regarding your request function, you tries to download a binary file as text (the default behavior of xhr requests). You should add httpRequest.responseType = "arraybuffer"; and use this.response instead of this.responseText.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Stuk/jszip/issues/347#issuecomment-242905564, or mute the thread https://github.com/notifications/unsubscribe-auth/AQ6YkWKb_1W0wu5ylcorjzo52yp8pD10ks5qj_sggaJpZM4JtnkV .

dduponchel commented 8 years ago

JSZip.version has been added in v3.1.0: if you get undefined then the wrong version is loaded (if zip.generateAsync is not a function, I'd say a v2.x.x).

Since you use a module loader, check of an other dependency doesn't already include/depends on JSZip and load the wrong version.

Your smile.html file doesn't load existing files (Download.js, jszip.js) but tries to load other (viewer/Controller.js, webservice_proxyAPP1.js, viewer_SMILE.js) so I can't test it on my side. I'm not asking for the full project, just for a minimal, complete, and verifiable example.

Regarding the request function, I'm talking about this one. Instead of doing

function request(url) {
  return new Promise(function(resolve) {
    var httpRequest = new XMLHttpRequest();
    httpRequest.open("GET", url);
    httpRequest.onload = function() {
      zip.file(url, this.responseText); // fine if you load text, but will corrupt binary files
      resolve();
    };
    httpRequest.send();
  });
}

you should do

function request(url) {
  return new Promise(function(resolve, reject) {
    var httpRequest = new XMLHttpRequest();
    httpRequest.open("GET", url);
    httpRequest.responseType = "arraybuffer"; // here, you ask for the binary content
    httpRequest.onload = function() {
      zip.file(url, this.response); // you will get an ArrayBuffer there
      resolve();
    };
    httpRequest.onerror = function() {
      // don't forget to reject the promise when an error occurs,
      // the promise will never finish otherwise
      reject(new Error("can't load " + url));
    };
    httpRequest.send();
  });
}
cici004 commented 8 years ago

Hi David,

Thanks a lot for your help! I attached the project folder.

You may want to look at 1.smile.html (Path: d4rowcmv1\viewer\smile.html)

  1. Download Folder: (Path: d4rowcmv1\viewer\js\gis\dijit\Download)
  2. jszip.js (Path: 4rowcmv1\viewer\jszip.js) 4. Download.js (Path: d4rowcmv1\viewer\js\gis\dijit\Download.js)

Many thanks,

-Xi

On Mon, Aug 29, 2016 at 11:33 AM, David Duponchel notifications@github.com wrote:

JSZip.version has been added in v3.1.0: if you get undefined then the wrong version is loaded (if zip.generateAsync is not a function, I'd say a v2.x.x).

Since you use a module loader, check of an other dependency doesn't already include/depends on JSZip and load the wrong version.

Your smile.html file doesn't load existing files (Download.js, jszip.js) but tries to load other (viewer/Controller.js, webservice_proxyAPP1.js, viewer_SMILE.js) so I can't test it on my side. I'm not asking for the full project, just for a minimal, complete, and verifiable example https://stackoverflow.com/help/mcve.

Regarding the request function, I'm talking about this one https://github.com/cici004/SMILE-Test/blob/master/Download.js#L206. Instead of doing

function request(url) { return new Promise(function(resolve) { var httpRequest = new XMLHttpRequest(); httpRequest.open("GET", url); httpRequest.onload = function() { zip.file(url, this.responseText); // fine if you load text, but will corrupt binary files resolve(); }; httpRequest.send(); }); }

you should do

function request(url) { return new Promise(function(resolve, reject) { var httpRequest = new XMLHttpRequest(); httpRequest.open("GET", url); httpRequest.responseType = "arraybuffer"; // here, you ask for the binary content httpRequest.onload = function() { zip.file(url, this.response); // you will get an ArrayBuffer there resolve(); }; httpRequest.onerror = function() { // don't forget to reject the promise when an error occurs, // the promise will never finish otherwise reject(new Error("can't load " + url)); }; httpRequest.send(); }); }```h

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Stuk/jszip/issues/347#issuecomment-243212916, or mute the thread https://github.com/notifications/unsubscribe-auth/AQ6YkeBzF0626lBXjTBLqpQolnU8sq--ks5qkyYPgaJpZM4JtnkV .

dduponchel commented 8 years ago

I don't see any attached document (here or in my inbox). Is it something github filters out when someone replies to an email or is it an oversight ?

cici004 commented 8 years ago

Hi David,

I sent my files again to your gmail account. Can you check them out?

-Xi

On Mon, Aug 29, 2016 at 4:04 PM, David Duponchel notifications@github.com wrote:

I don't see any attached document (here or in my inbox). Is it something github filters out when someone replies to an email or is it an oversight ?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Stuk/jszip/issues/347#issuecomment-243284172, or mute the thread https://github.com/notifications/unsubscribe-auth/AQ6YkbPh_n8daU5xaiTXZj0ccR5mnxrXks5qk2WPgaJpZM4JtnkV .

cici004 commented 8 years ago

FYI.

  1. I am using the latest version v3.1.2 but for no reason, it kept returning the error of Uncaught (in promise) TypeError: zip.generateAsync is not a function(…) Download.js:219
  2. I searched "Jszip" among all my files and did not find any conflict.

On Tue, Aug 30, 2016 at 7:55 AM, Xi Han xihancooking@gmail.com wrote:

Hi David,

I sent my files again to your gmail account. Can you check them out?

-Xi

On Mon, Aug 29, 2016 at 4:04 PM, David Duponchel <notifications@github.com

wrote:

I don't see any attached document (here or in my inbox). Is it something github filters out when someone replies to an email or is it an oversight ?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Stuk/jszip/issues/347#issuecomment-243284172, or mute the thread https://github.com/notifications/unsubscribe-auth/AQ6YkbPh_n8daU5xaiTXZj0ccR5mnxrXks5qk2WPgaJpZM4JtnkV .

dduponchel commented 8 years ago

In your file viewer/js/gis/dijit/Export.js, you have

require(['//cdnjs.cloudflare.com/ajax/libs/xlsx/0.7.8/xlsx.core.min.js']);

This library depends on JSZip v2.4.0, which is the version you get.

I see two solutions:

The API are not compatible, you can't remove the v2 (or else js-xlsx will stop working).

I never used the dojo amd loader so the following code may work, you should test it !

You can do alias it with paths:

var dojoConfig = {
  // ...
  paths: {'jszip_v3': 'https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.2/jszip'}
  // I tried with 'js/jszip' but it resolved to http://js.arcgis.com/3.17compact/dojo/js/jszip.js
  // I'll let that up to you
}

Then, When you need JSZip v3:

define([
  'jszip_v3',
  // ...
], function (
  JSZipv3,
  // ...
) {
  console.log("loaded jszip v", JSZipv3.version); //  will display loaded jszip v 3.1.2

When I tried, the global JSZip object is still the v2.4 (which is what you want): the xlsx export should work but I encourage you to test it.

cici004 commented 8 years ago

Thanks a lot for your help David. I am gonna test on it soon!

On Tue, Aug 30, 2016 at 11:12 AM, David Duponchel notifications@github.com wrote:

In your file viewer/js/gis/dijit/Export.js, you have

require(['//cdnjs.cloudflare.com/ajax/libs/xlsx/0.7.8/xlsx.core.min.js']);

This library depends on JSZip v2.4.0, which is the version you get.

I see two solutions:

  • use JSZip v2
  • alias the v3 to an other name and require it

The API are not compatible, you can't remove the v2 (or else js-xlsx will stop working).

I never used the dojo amd loader so the following code may work, you should test it !

You can do alias it with paths https://dojotoolkit.org/reference-guide/1.7/dojo/_base/config.html#finding-resources-in-non-standard-locations :

var dojoConfig = { // ... paths: {'jszip_v3': 'https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.2/jszip'} // I tried with 'js/jszip' but it resolved to http://js.arcgis.com/3.17compact/dojo/js/jszip.js // I'll let that up to you }

Then, When you need JSZip v3:

define([ 'jszip_v3', // ... ], function ( JSZipv3, // ... ) { console.log("loaded jszip v", JSZipv3.version); // will display loaded jszip v 3.1.2

When I tried, the global JSZip object is still the v2.4 (which is what you want): the xlsx export should work but I encourage you to test it.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Stuk/jszip/issues/347#issuecomment-243529377, or mute the thread https://github.com/notifications/unsubscribe-auth/AQ6YkarFmMPssomiuq9RGmMopYv8XhKVks5qlHJ7gaJpZM4JtnkV .

cici004 commented 8 years ago

Hi David,

I have questions still confuse me. Sorry about that.

  1. you mentioned that In my file viewer/js/gis/dijit/Export.js, I have

require(['//cdnjs.cloudflare.com/ajax/libs/xlsx/0.7.8/xlsx.core.min.js']);, it does exist, but it was commented out in the original document. I do not see how this influences the running.

  1. when you suggested to add the following code. I do not know are they going to be located?

    var dojoConfig = {

    // ... paths: {'jszip_v3': 'https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.2/jszip'} // I tried with 'js/jszip' but it resolved to http://js.arcgis.com/3.17compact/dojo/js/jszip.js // I'll let that up to you }

Then, When you need JSZip v3:

define([ 'jszip_v3', // ... ], function ( JSZipv3, // ... ) { console.log("loaded jszip v", JSZipv3.version); // will display loaded jszip v 3.1.2

Thanks,

Xi

On Tue, Aug 30, 2016 at 4:21 PM, Xi Han xihancooking@gmail.com wrote:

Thanks a lot for your help David. I am gonna test on it soon!

On Tue, Aug 30, 2016 at 11:12 AM, David Duponchel < notifications@github.com> wrote:

In your file viewer/js/gis/dijit/Export.js, you have

require(['//cdnjs.cloudflare.com/ajax/libs/xlsx/0.7.8/xlsx.core.min.js']);

This library depends on JSZip v2.4.0, which is the version you get.

I see two solutions:

  • use JSZip v2
  • alias the v3 to an other name and require it

The API are not compatible, you can't remove the v2 (or else js-xlsx will stop working).

I never used the dojo amd loader so the following code may work, you should test it !

You can do alias it with paths https://dojotoolkit.org/reference-guide/1.7/dojo/_base/config.html#finding-resources-in-non-standard-locations :

var dojoConfig = { // ... paths: {'jszip_v3': 'https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.2/jszip'} // I tried with 'js/jszip' but it resolved to http://js.arcgis.com/3.17compact/dojo/js/jszip.js // I'll let that up to you }

Then, When you need JSZip v3:

define([ 'jszip_v3', // ... ], function ( JSZipv3, // ... ) { console.log("loaded jszip v", JSZipv3.version); // will display loaded jszip v 3.1.2

When I tried, the global JSZip object is still the v2.4 (which is what you want): the xlsx export should work but I encourage you to test it.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Stuk/jszip/issues/347#issuecomment-243529377, or mute the thread https://github.com/notifications/unsubscribe-auth/AQ6YkarFmMPssomiuq9RGmMopYv8XhKVks5qlHJ7gaJpZM4JtnkV .

jimmywarting commented 8 years ago

PS: next time you want to post some code wrap it with ```

> ```javascript
> code block
> ```
dduponchel commented 8 years ago

it does exist, but it was commented out in the original document.

I see the loadXLSXParser function (which imports js-xlsx) in viewer/js/gis/dijit/Export.js and in viewer/js/gis/dijit/Download.js. Are they both commented ?

I do not know are they going to be located?

dojoConfig already exists in your code: see index.html and/or viewer/smile.html.

cici004 commented 6 years ago

Hi Stuk,

Check out the books I added on Goodreads.

http://www.goodreads.com/friend/i?i=LTM1MzQzOTIzNzQ6NDEz&e=reply@reply.github.com&n=Stuk jszip&utm_medium=email&utm_source=invite

Goodreads is a community for book lovers. It's a great way to get book recommendations from your friends and others. You can keep a list of books to read, join book clubs, and even take the never-ending book trivia quiz.


To opt-out of future invites to Goodreads please follow this link: http://www.goodreads.com/user/block_email?email_to_block=R29XUXNrZGlBalhLN0JDZWhHV2NJeHBNMlNXYUFncmNoSXRBN1h0OXZUNko5Q2tRV3dIMUVvSE1oZ29JQU90MEhsTk53Mm9tcjBlazR5YVowVXZWdG90UzZDUkRxd09QbkdUZmc3dktvMmtKa0Q1RW5ScTY5bExBT0g5OFkySHNDckh3ZVZqWHcvUFFnQmdxZ0FvRU45QmlWUVFqUVI3b0FZWXBvV0RGeGVvPS0tTi8xR0dNVE9KaEhyRDlFOUROTVVqUT09--585f02aa9beefce97fce873a628fca2105ce5ea9&utm_content=A&utm_medium=email&utm_source=invite

This email was sent by request to reply@reply.github.com.

(c) 2017 Goodreads, Inc. 188 Spear Street, 3rd Floor, San Francisco, CA 94105

cici004 commented 6 years ago

Hi Stuk,

Check out the books I added on Goodreads.

http://www.goodreads.com/friend/i?i=LTM1MzQzOTIzNzQ6NDEz&e=reply@reply.github.com&n=Stuk jszip&utm_medium=email&utm_source=invite

Goodreads is a community for book lovers. It's a great way to get book recommendations from your friends and others. You can keep a list of books to read, join book clubs, and even take the never-ending book trivia quiz.


To opt-out of future invites to Goodreads please follow this link: http://www.goodreads.com/user/block_email?email_to_block=UUJoVVE2SllzRk9RSE8yUWI2VUFzai9pQ1Vid045REZ5aEhmYnZSeVNmY3pUMmcydVc4Ym5vZlVnbEd2OVVOd1ZWczlqMFVsdEhhSU9HaUYvWGs4UG4yYStmT2x3aXUzNnhrWTZjV2IxandJRStFajBGUHQ4M2VoZnE3MVlwMlpURUtxNzQrbHRqcTU3NHo0UW43SkZudk5iRGdMRldCY2cxek9KS2dNY2k4PS0tQW11cHdtQmdmSllsUU5wVFhxUG1kZz09--68183915132bc07fb77afb5a172cbee0433aa1d8&utm_content=A&utm_medium=email&utm_source=invite

This email was sent by request to reply@reply.github.com.

(c) 2017 Goodreads, Inc. 188 Spear Street, 3rd Floor, San Francisco, CA 94105

cici004 commented 6 years ago

Hi Stuk,

Check out the books I added on Goodreads.

http://www.goodreads.com/friend/i?i=LTM1MzQzOTIzNzQ6NDEz&e=reply@reply.github.com&n=Stuk jszip&utm_medium=email&utm_source=invite

Goodreads is a community for book lovers. It's a great way to get book recommendations from your friends and others. You can keep a list of books to read, join book clubs, and even take the never-ending book trivia quiz.


To opt-out of future invites to Goodreads please follow this link: http://www.goodreads.com/user/block_email?email_to_block=bW5OUGVHTVQ3K3pya3hveVcvcEpGbG5pSjFqdGV2Qm40MVFZVjVFMzFPcEloYUJxbnltSXJBNXFZVUpJY2VVWXNhYVFoYTZKYVdGSk9VdjBFelNpNnQ0TWlDNjhhZXdZSFBkcWZVQVJtbldvZHFHeFhzT0VpcDdnTjlnZE1OYUxpTFB3aTBTbFdORDROa1hCRUhPclA4cGlKUlVGYlI3TytTbXV3TGpLUVQ4PS0tUzJRcTRIMnF4ZEQ5S3dEdmhZNSthdz09--fef04b9ceb69120c398bfd9c71008c8850b49aef&utm_content=A&utm_medium=email&utm_source=invite

This email was sent by request to reply@reply.github.com.

(c) 2017 Goodreads, Inc. 188 Spear Street, 3rd Floor, San Francisco, CA 94105

cici004 commented 6 years ago

Hi Stuk,

Check out the books I added on Goodreads.

http://www.goodreads.com/friend/i?i=LTM1MzQzOTIzNzQ6NDEz&e=reply@reply.github.com&n=Stuk jszip&utm_medium=email&utm_source=invite

Goodreads is a community for book lovers. It's a great way to get book recommendations from your friends and others. You can keep a list of books to read, join book clubs, and even take the never-ending book trivia quiz.


To opt-out of future invites to Goodreads please follow this link: http://www.goodreads.com/user/block_email?email_to_block=UGZpS05OTmxqdFU3NTNVZW92VEwyaVViRzNaUStmckRBQjdabDFoT0hnTzlxU1FwR3BBYXErUjN4ajZBL2RnVUdnMlJMYk44SmZQa1lqdkgwK3d2Ri9QOEF6Rk9Fb29zY21CY1loeTdsUGZUTEhEa0VyMzllSEpiMndBYzRucEVxNVdKUUJaRzNEanB5bEVRSjZLZzVXMTA2cjdaM0ZvNXJtYjFPZy9LL1JBPS0tTXFHUWZvQXdFT1E3WXRVMzQyN2t2QT09--ef19464366c34830b8bb1135b57327cb5d99cecd&utm_content=A&utm_medium=email&utm_source=invite

This email was sent by request to reply@reply.github.com.

(c) 2017 Goodreads, Inc. 188 Spear Street, 3rd Floor, San Francisco, CA 94105

ravisuryawanshi18 commented 6 years ago

I am also getting the same error, Can any one help?

fetch("/downloadPayload", { method: "post", headers: new Headers({ 'Cache-Control':'no-cache', 'Pragma':'no-cache', 'Content-Type': 'application/x-www-form-urlencoded', }),

        body: this.jsonToUrlParams({
            'transactionIds': transArr,
            'type': 'Intersection',
            'partnerEntName': 'SupplierA'
        })

    }).then(function(response) {
        return response.json();
    }, function(error) {
        console.error('error while downloading zip');
    }).then(function(result) {

        JSZip.generateAsync({type:"string"})
            .then(function (result) {
                // see FileSaver.js
            saveAs(result, "payload.zip");
        });
    })