Skip to content

Instantly share code, notes, and snippets.

@userddssilva
Created March 16, 2021 13:48
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 userddssilva/087efa9e4f47a916ba46a68f4da2fcd3 to your computer and use it in GitHub Desktop.
Save userddssilva/087efa9e4f47a916ba46a68f4da2fcd3 to your computer and use it in GitHub Desktop.
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
// Start file download.
download("hello.txt","This is the content of my file :)");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment