Skip to content

Instantly share code, notes, and snippets.

@zh012
Created June 3, 2015 13:16
Show Gist options
  • Select an option

  • Save zh012/b0e90fe3a4d359459027 to your computer and use it in GitHub Desktop.

Select an option

Save zh012/b0e90fe3a4d359459027 to your computer and use it in GitHub Desktop.
// from http://stackoverflow.com/a/29304414
var download = function(content, fileName, mimeType) {
var a = document.createElement('a');
if (navigator.msSaveBlob) { // IE10
return navigator.msSaveBlob(new Blob([content], { type: mimeType }), fileName);
} else if ('download' in a) { //html5 A[download]
a.href = 'data:' + mimeType + ',' + encodeURIComponent(content);
a.setAttribute('download', fileName);
document.body.appendChild(a);
setTimeout(function() {
a.click();
document.body.removeChild(a);
}, 100);
return true;
} else { //do iframe dataURL download (old ch+FF):
var f = document.createElement('iframe');
document.body.appendChild(f);
f.src = 'data:' + mimeType + ',' + encodeURIComponent(content);
setTimeout(function() {
document.body.removeChild(f);
}, 500);
return true;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment