How to generate and prompt to save a file from content in the client browser? [duplicate]

This “FileSaver” library may help. If you want it to be reasonably cross-browser, you’ll also need this to implement the W3C Blob API in places it’s not already implemented. Both respect namespaces, and are completely framework agnostic, so don’t worry about naming issues.

Once you’ve got those included, and as long as you’re only saving text files, you should be able to

var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
saveAs(blob, "hello world.txt");

Note that the first argument to new Blob has to be a list of strings, and that you’re expected to specify the filename. As in, the user will see this file being downloaded locally, but won’t be able to name it themselves. Hopefully they’re using a browser that handles local filename collisions…

Leave a Comment