Skip to content

Instantly share code, notes, and snippets.

@jpillora
Created May 28, 2013 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpillora/5660222 to your computer and use it in GitHub Desktop.
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
// 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