Skip to content

Instantly share code, notes, and snippets.

@hahn-kev
Forked from liabru/save-file-local.js
Created September 13, 2020 21:38
Show Gist options
  • Save hahn-kev/3cc96f4e126447cfd13cd31fc15953f3 to your computer and use it in GitHub Desktop.
Save hahn-kev/3cc96f4e126447cfd13cd31fc15953f3 to your computer and use it in GitHub Desktop.
Save a text file locally with a filename, by triggering a download in JavaScript
/*
* Save a text file locally with a filename by triggering a download
*/
var text = "hello world",
blob = new Blob([text], { type: 'text/plain' }),
anchor = document.createElement('a');
anchor.download = "hello.txt";
anchor.href = (window.webkitURL || window.URL).createObjectURL(blob);
anchor.dataset.downloadurl = ['text/plain', anchor.download, anchor.href].join(':');
anchor.click();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment