Update JSON API
[src/app-framework-demo.git] / afm-client / bower_components / jszip / documentation / howto / write_zip.md
1 ---
2 title: "How to write a file / give it to the user"
3 layout: default
4 section: example
5 ---
6
7 ### In the browser
8
9 With only javascript, this part won't work in old browsers, including IE < 10.
10 For those browsers, you can use a flash polyfill, see below.
11
12 You can also see this
13 [example]({{site.baseurl}}/documentation/examples/download-zip-file.html).
14
15 #### Blob URL / FileSaver
16
17 With recent browsers, the easiest way is to use `saveAs` or a polyfill, see
18 [FileSaver.js](https://github.com/eligrey/FileSaver.js) :
19
20 ```js
21 var blob = zip.generate({type:"blob"});
22 saveAs(blob, "hello.zip");
23 ```
24
25 Under the hood, the polyfill uses the native `saveAs` from the
26 [FileSaver](http://www.w3.org/TR/file-writer-api/#the-filesaver-interface) API
27 (on Chrome and IE10+) or use a [Blob URL](http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download)
28 (on Firefox).
29
30
31 #### Data URI
32
33 For older browsers that support [data URI](http://caniuse.com/datauri), you can also
34 do the following :
35
36 ```js
37 location.href="data:application/zip;base64," + zip.generate({type:"base64"});
38 ```
39
40 The biggest issue here is that the filenames are very awkward, Firefox
41 generates filenames such as `a5sZQRsx.zip.part` (see bugs
42 [367231](https://bugzilla.mozilla.org/show_bug.cgi?id=367231) and
43 [532230](https://bugzilla.mozilla.org/show_bug.cgi?id=532230), and Safari
44 isn't much better with just `Unknown`.
45
46 Browser support and resulting filename :
47
48 Opera  | Firefox | Safari | Chrome | Internet Explorer
49 -------|---------|--------|--------|------------------
50 "default.zip" | random alphanumeric with ".part" extension | "Unknown" (no extension) | "download.zip" on OSX and Linux, just "download" on Windows | No
51
52 #### Downloadify
53
54 [Downloadify](https://github.com/dcneiner/downloadify) uses a small Flash SWF
55 to download files to a user's computer with a filename that you can choose.
56 Doug Neiner has added the `dataType` option to allow you to pass a zip for
57 downloading. Follow the [Downloadify demo](http://pixelgraphics.us/downloadify/test.html)
58 with the following changes:
59
60 ```js
61 zip = new JSZip();
62 zip.file("Hello.", "hello.txt");
63 Downloadify.create('downloadify',{
64 ...
65   data: function(){
66     return zip.generate({type:"base64"});
67   },
68 ...
69   dataType: 'base64'
70 });
71 ```
72
73 <!--
74 TODO : send data as GET / POST ?
75 -->
76
77 #### Deprecated google gears
78
79 [Franz Buchinger](http://www.picurl.org/blog/author/franz/) has written a
80 brilliant tutorial on [using JSZip with Google Gears](http://www.picurl.org/blog/2009/11/22/creating-zip-archives-with-gears)
81 ([part 2](http://www.picurl.org/blog/2009/11/29/gearszipper-part2-adding-support-for-real-files-and-canvas-elements/)).
82 If you want to let your Gears users download several files at once I really
83 recommend having a look at some of his [examples](http://picurl.org/gears/zipper/).
84
85
86
87 ### In nodejs
88
89 JSZip can generate Buffers so you can do the following :
90
91 ```js
92 var fs = require("fs");
93 var JSZip = require("jszip");
94
95 var zip = new JSZip();
96 // zip.file("file", content);
97 // ... and other manipulations
98
99 var buffer = zip.generate({type:"nodebuffer"});
100
101 fs.writeFile("test.zip", buffer, function(err) {
102   if (err) throw err;
103 });
104 ```
105
106