Search code examples
jqueryjszip

issue with JSZip 3.0


I have an error with JSZip 3.0. I am making a code for creating and download zip for that I am using the JSZip library.

and my code to create a zip is given below.

var zip = new JSZip();
var urls = ["image/Screenshot (11).png","image/Screenshot (10).png"];
var count = 0;
var zipFilename = "zipFilename.zip";
urls.forEach(function(url){
  var filename = "filename";
  JSZipUtils.getBinaryContent(url, function (err, data) {
     if(err) {
        throw err; // or handle the error
     }
     zip.file(filename, data, {binary:true});
     count++;
     if (count == urls.length) {
       var zipFile = zip.generate({type: "blob"});
       saveAs(zipFile, zipFilename);
     }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.2.1/jszip.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip-utils/0.1.0/jszip-utils.js"></script>

with that I have received error Uncaught Error: Error: This method has been removed in JSZip 3.0, please check the upgrade guide. at XMLHttpRequest.xhr.onreadystatechange

when I research this error I have found one answer This but I don't understand how and where can I use this.

can anybody help me with this.


Solution

  • Try this code

    <html>
    <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.2.1/jszip.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip-utils/0.1.0/jszip-utils.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.min.js"></script>
    <script type="text/javascript">
    var zip = new JSZip();
    var urls = ["logo.png","foot-logo.png"];
    var count = 0;
    var zipFilename = "zipFilename.zip";
    urls.forEach(function(url){
      var filename = "filename";
      console.log(url);
      JSZipUtils.getBinaryContent(url, function (err, data) {
    
         if(err) {
            throw err; // or handle the error
         }
         zip.file(url, data, {binary:true});
         count++;
         if (count == urls.length) {
           zip.generateAsync({type:"blob"})
    .then(function (blob) {
        saveAs(blob, zipFilename);
    });
         }
      });
    });
    </script>
    </head>
    <body>
    Javascript zip file
    </body>
    </html>