jszip
Version:
Create, read and edit .zip files with Javascript http://stuartk.com/jszip
76 lines (68 loc) • 2.28 kB
HTML
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>JSZip example : download the generated zip file</title>
<link media="screen" href="style.css" type="text/css" rel="stylesheet">
</head>
<body>
<h1><a href="../">JSZip</a> example : download the generated zip file</h1>
<p>Tip : check the source of the page !</p>
<h2>The data URL</h2>
<div>
Does not work in IE, has restrictions on the length.<br/>
<a href="#" id="data_uri">click to download</a>
</div>
<h2>The Blob URL / saveAs</h2>
<div>
Works on firefox, chrome , opera >= 15 and IE >= 10 (but NOT in compatibility view).<br/>
<a href="#" id="blob">click to download</a>
</div>
<script type="text/javascript" src="../dist/jszip.js"></script>
<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);
}
}
// data URI
function downloadWithDataURI() {
dataUriLink.href = "data:application/zip;base64," + zip.generate({type:"base64"});
dataUriLink.download = "hello.zip";
}
var dataUriLink = document.getElementById('data_uri');
bindEvent(dataUriLink, 'click', downloadWithDataURI);
// Blob
var blobLink = document.getElementById('blob');
var saveAs = window.saveAs || (navigator.msSaveBlob && navigator.msSaveBlob.bind(navigator))
var blobDownloadSupported = JSZip.support.blob && (saveAs || window.URL && window.URL.createObjectURL);
if (blobDownloadSupported) {
function downloadWithBlob() {
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;
}
bindEvent(blobLink, 'click', downloadWithBlob);
} else {
blobLink.innerHTML += " (not supported on this browser)";
}
})();
</script>
</body>
</html>