Update JSON API
[src/app-framework-demo.git] / afm-client / bower_components / jszip / documentation / examples / download-zip-file.html
1 ---
2 title: "Download the generated zip file"
3 layout: default
4 section: example
5 ---
6
7 <p>Tip : check the source of the page !</p>
8 <h2>The FileSaver API</h2>
9 <div>
10   Works on firefox, chrome , opera &gt;= 15 and IE &gt;= 10 (but NOT in compatibility view).<br/>
11   <button id="blob" class="btn btn-primary">click to download</button>
12 </div>
13 <h2>The data URL</h2>
14 <div>
15   Does not work in IE, has restrictions on the length.<br/>
16   <button id="data_uri" class="btn btn-primary">click to download</button>
17 </div>
18 <script type="text/javascript">
19 (function () {
20   var zip = new JSZip();
21   zip.file("Hello.txt", "Hello world\n");
22
23   function bindEvent(el, eventName, eventHandler) {
24     if (el.addEventListener){
25       // standard way
26       el.addEventListener(eventName, eventHandler, false);
27     } else if (el.attachEvent){
28       // old IE
29       el.attachEvent('on'+eventName, eventHandler);
30     }
31   }
32
33   // Blob
34   var blobLink = document.getElementById('blob');
35   if (JSZip.support.blob) {
36     function downloadWithBlob() {
37       try {
38         var blob = zip.generate({type:"blob"});
39         // see FileSaver.js
40         saveAs(blob, "hello.zip");
41       } catch(e) {
42         blobLink.innerHTML += " " + e;
43       }
44       return false;
45     }
46     bindEvent(blobLink, 'click', downloadWithBlob);
47   } else {
48     blobLink.innerHTML += " (not supported on this browser)";
49   }
50
51   // data URI
52   function downloadWithDataURI() {
53     window.location = "data:application/zip;base64," + zip.generate({type:"base64"});
54   }
55   var dataUriLink = document.getElementById('data_uri');
56   bindEvent(dataUriLink, 'click', downloadWithDataURI);
57
58 })();
59 </script>