Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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();
@cirocorvino

This comment has been minimized.

Copy link

cirocorvino commented Jun 6, 2018

thanks is a great solution

@technosmarter

This comment has been minimized.

Copy link

technosmarter commented Jul 26, 2018

In the network tab of the dev tool, this URL fails: //pagead2.googlesyndication.com/pagead/adsbygoogle.js It triggers a .txt file download. –
Anyone, please help me ....My Link

@jbreckmckye

This comment has been minimized.

Copy link

jbreckmckye commented Sep 7, 2018

Don't forget to revoke the object URL to free the memory - they don't get garbage collected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.