From 62995090d9d752a774d020c0ee64771b38e5bcbf Mon Sep 17 00:00:00 2001
From: David Duponchel Tip : check the source of the page ! With some recent browsers come a new way to download Blobs (a zip file for example) : blob urls. The download attribute on <a> allows you to give the name of the file. Blob urls start to be widely supported but this attribute is currently only supported in Chrome and Firefox (>= 20). See the example. With some recent browsers come a new way to download Blobs (a zip file for example) : blob urls. The download attribute on <a> allows you to give the name of the file. Blob urls start to be widely supported but this attribute is currently only supported in Chrome and Firefox (>= 20). See the example. Note : on IE 10/11, using a blob url as a href doesn't seem to work. An other solution is the FileSaver interface, created (at the time of writing) with Finally, you can use the polyfill FileSaver.js which will use a FileSaver if present or else a blob url.
Franz Buchinger has written a brilliant tutorial on using JSZip with Google Gears (part 2). If you want to let your Gears users download several files at once I really recommend having a look at some of his examples. An other solution is the FileSaver interface, created (at the time of writing) with An other solution is the FileSaver interface, created (at the time of writing) with JSZip example : download the generated zip file
The data URL
click to download
The Blob URL
+The Blob URL / saveAs
click to download
The Blob URL
zip.file("Hello.txt", "Hello world\n");
// data URI
- document.getElementById('data_uri').href = "data:application/zip;base64," + zip.generate();
+ var dataUriLink = document.getElementById('data_uri');
+ dataUriLink.onclick = function () {
+ dataUriLink.href = "data:application/zip;base64," + zip.generate({type:"base64"});
+ dataUriLink.download = "hello.zip";
+ }
// Blob
var blobLink = document.getElementById('blob');
- try {
- blobLink.download = "hello.zip";
- blobLink.href = window.URL.createObjectURL(zip.generate({type:"blob"}));
- } catch(e) {
+ var saveAs = window.saveAs || (navigator.msSaveBlob && navigator.msSaveBlob.bind(navigator))
+ var blobDownloadSupported = JSZip.support.blob && (saveAs || window.URL && window.URL.createObjectURL);
+ if (blobDownloadSupported) {
+ blobLink.onclick = function () {
+ try {
+ var blob = zip.generate({type:"blob"});
+ if (saveAs) {
+ saveAs(blob, "hello.zip");
+ } else {
+ blobLink.download = "hello.zip";
+ blobLink.href = window.URL.createObjectURL(blob);
+ }
+ } catch(e) {
+ blobLink.innerHTML += " " + e;
+ }
+ return false;
+ };
+ } else {
blobLink.innerHTML += " (not supported on this browser)";
}
})();
From 159dda7d087970de3124272a2429c9d5a1ecc996 Mon Sep 17 00:00:00 2001
From: David Duponchel Solution-ish: Downloadify<
dataType: 'base64'
});
-
Other solution-ish: Blob URL
- Other solution-ish: Blob URL / FileSaver / FileSaver.js
+
var blob = zip.generate({type:"blob"});
myLink.href = window.URL.createObjectURL(blob);
myLink.download = "myFile.zip";
+ saveAs
. This works on Chrome and IE >= 10 but not Firefox.
+var blob = zip.generate({type:"blob"});
+window.saveAs(blob, "hello.zip");
+
+ Usage with Google Gears
The data URL
The Blob URL / saveAs
+ Works on firefox, chrome , opera >= 15 and IE >= 10 (but NOT in compatibility view).
click to download
Other solution-ish: Blob URL / FileSaver / FileSaver.js
myLink.href = window.URL.createObjectURL(blob);
myLink.download = "myFile.zip";
- saveAs
. This works on Chrome and IE >= 10 but not Firefox.saveAs
. This works on Chrome and IE >= 10 (not in compatibility view) but not Firefox.
var blob = zip.generate({type:"blob"});
window.saveAs(blob, "hello.zip");
From 973b44a9f62c097c7f33e68e94ff24d073980758 Mon Sep 17 00:00:00 2001
From: David Duponchel From 1.x to 2.x
// before
zip.file("test.txt").data;
zip.files["test.txt"].data;
+zip.file("image.png").data;
+zip.files["image.png"].data;
// after
zip.file("test.txt").asText();
zip.files["test.txt"].asText();
+zip.file("image.png").asBinary();
+zip.files["image.png"].asBinary();
+var zip = new JSZip(); +// same as +var zip = JSZip();-
load()
for more details and this for the limitations.new JSZip(zipDataFromXHR, {base64:false}); // same as +JSZip(zipDataFromXHR, {base64:false}); +// same as var zip = new JSZip(); zip.load(zipDataFromXHR, {base64:false});