Created
May 28, 2013 02:45
-
-
Save jpillora/5660222 to your computer and use it in GitHub Desktop.
Chrome text download (<a> download attribute) example
For use with forced-server-download method as fallback
This file contains 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
// Chrome text download (<a> download attribute) example | |
// For use with forced-server-download method as fallback | |
// extracted from https://github.com/eligrey/FileSaver.js | |
function saveAs(name, text) { | |
var a = document.createElementNS("http://www.w3.org/1999/xhtml", "a"); | |
if(!("download" in a)) | |
return false; | |
var blob = new window.Blob([text], {type: "text/plain;charset=utf8"}); | |
a.href = window.URL.createObjectURL(blob); | |
a.download = name; | |
var event = document.createEvent("MouseEvents"); | |
event.initMouseEvent("click", 1, 0, window, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null); | |
a.dispatchEvent(event); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment