Javascript: Save text or string as a local file

This isn't hard. You use HTML5's download attribute on an a link.

var a = document.createElement("a");
a.href="data:application/json;charset=utf-8,YOUR_TEXT_HERE" = "some.json"

In this case I'm saving it as JSON. Your file turns up in the Downloads directory.

You can't seem to specify the location though, either in code or via a popup - likely due to security concerns.


Edit on github
comments powered by Disqus
Click me