excel-builder
Version:
An easy way of building Excel files with javascript
62 lines (56 loc) • 1.8 kB
HTML
---
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 >= 15 and IE >= 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() {
zip.generateAsync({type:"blob"}).then(function (blob) {
saveAs(blob, "hello.zip");
}, function (err) {
blobLink.innerHTML += " " + err;
});
return false;
}
bindEvent(blobLink, 'click', downloadWithBlob);
} else {
blobLink.innerHTML += " (not supported on this browser)";
}
// data URI
function downloadWithDataURI() {
zip.generateAsync({type:"base64"}).then(function (base64) {
window.location = "data:application/zip;base64," + base64;
}, function (err) {
// shouldn't happen with a base64...
});
}
var dataUriLink = document.getElementById('data_uri');
bindEvent(dataUriLink, 'click', downloadWithDataURI);
})();
</script>