Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

saveAs is not defined when using JSzip

I am getting an error when trying to do a simple jszip

Uncaught (in promise) ReferenceError: saveAs is not defined

Pretty sure I included all of the correct files so I am not sure what I am doing wrong, could someone please enlighten me?

    <!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Download</title>
    <link rel="stylesheet" href="css/download.css">
    <script type="text/javascript" src="jszip/dist/jszip.js"></script>
    <script type="text/javascript" src="https://stuk.github.io/jszip-utils/dist/jszip-utils.js"></script>
    <script src='https://code.jquery.com/jquery-2.2.4.min.js'></script>
    <script src="js/download.js"></script>
    <link href="https://fonts.googleapis.com/css?family=Josefin+Sans" rel="stylesheet">
  </head>

  <body>
  <div class="maindiv"> 
    <header>
      <a href="index.html" class="company">Instasample</a>
      <a href="login" class="login">Login</a>
    </header>
    <div class="soulection">
    <button id="kick" onclick="download(this.id)" >kick</button>
    <button id="snare" onclick="download(this.id)" >snare</button>
    <button id="hat" onclick="download(this.id)" >hat</button>
    <button id="open hat" onclick="download(this.id)" >open hat</button>
    <button id="rim" onclick="download(this.id)" >rim</button>
    <button id="perc" onclick="download(this.id)" >perc</button>
    <button id="tom" onclick="download(this.id)" >tom</button> 
    <button id="clap" onclick="download(this.id)" >clap</button>
    <button id="foley" onclick="download(this.id)" >foley</button>
    <button id="ambient" onclick="download(this.id)" >ambient</button>
    <button id="effects" onclick="download(this.id)" >effects</button>
    <button id="vocal" onclick="download(this.id)" >vocal</button>
    <button id="synth" onclick="download(this.id)" >synth</button>
    <button id="808" onclick="download(this.id)" >808</button>
    <button id="bass" onclick="download(this.id)" >bass</button>
    <button id="sample" onclick="download(this.id)" >sample</button>
    </div>
    </div>
  </body>
  <script type="text/javascript">
    (function () {
  var zip = new JSZip();
  zip.file("Hello.txt", "Hello world\n");

  function bindEvent(el, eventName, eventHandler) {
    if (el.addEventListener){
      // standard way
      el.addEventListener(eventName, eventHandler, false);
    } else if (el.attachEvent){
      // old IE
      el.attachEvent('on'+eventName, eventHandler);
    }
  }

  // Blob
  var blobLink = document.getElementById('kick');
  if (JSZip.support.blob) {
    function downloadWithBlob() {
      zip.generateAsync({type:"blob"}).then(function (blob) {
        saveAs(blob, "hello.zip");
      }, function (err) {
          blobLink.innerHTML += " " + err;
      });
      return false;
    }
    bindEvent(blobLink, 'click', downloadWithBlob);
  } else {
    blobLink.innerHTML += " (not supported on this browser)";
  }

})();
  </script>
<footer>
  <a href="mailto:[email protected]?Subject=Hey%20Instasample" class="bottom">contact</a>
  <t class="bottomline">&nbsp;|&nbsp;</t>
  <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=5LC9QK23C9M28" class="bottom2">donate</a>
  <t class="bottomline">&nbsp;|&nbsp;</t>
  <t class="bottom3">&copy 2016</t>
</footer>

</html>
like image 551
Nick Garver Avatar asked Jan 17 '17 18:01

Nick Garver


People also ask

What is JavaScript saveAs?

Learn how to create a saveAs function to download one or multiple files in JavaScript. updated on November 21, 2021 November 14, 2021 By Muhi Masri. In the web world, saving a file or “Save As” means downloading a file to the client's machine. There isn't a built-in method in JavaScript that allows us to do so.


2 Answers

There is a comment in their example:

// see FileSaver.js

You need that module if you want to use saveAs()

Here it is: https://github.com/eligrey/FileSaver.js

like image 76
zord Avatar answered Oct 03 '22 04:10

zord


It works after adding the following script tag in the HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.js"></script>

like image 37
KuboAndTwoStrings Avatar answered Oct 01 '22 04:10

KuboAndTwoStrings