Created
June 3, 2015 13:16
-
-
Save zh012/b0e90fe3a4d359459027 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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