summaryrefslogtreecommitdiffstats
path: root/afm-client/bower_components/jszip/documentation/examples/download-zip-file.html
blob: 869a54e09d334b96022bf951df77b22da50e1792 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
---
title: "Download the generated zip file"
layout: default
section: example
---

<p>Tip : check the source of the page !</p>
<h2>The FileSaver API</h2>
<div>
  Works on firefox, chrome , opera &gt;= 15 and IE &gt;= 10 (but NOT in compatibility view).<br/>
  <button id="blob" class="btn btn-primary">click to download</button>
</div>
<h2>The data URL</h2>
<div>
  Does not work in IE, has restrictions on the length.<br/>
  <button id="data_uri" class="btn btn-primary">click to download</button>
</div>
<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('blob');
  if (JSZip.support.blob) {
    function downloadWithBlob() {
      try {
        var blob = zip.generate({type:"blob"});
        // see FileSaver.js
        saveAs(blob, "hello.zip");
      } catch(e) {
        blobLink.innerHTML += " " + e;
      }
      return false;
    }
    bindEvent(blobLink, 'click', downloadWithBlob);
  } else {
    blobLink.innerHTML += " (not supported on this browser)";
  }

  // data URI
  function downloadWithDataURI() {
    window.location = "data:application/zip;base64," + zip.generate({type:"base64"});
  }
  var dataUriLink = document.getElementById('data_uri');
  bindEvent(dataUriLink, 'click', downloadWithDataURI);

})();
</script>