Question

I have seen many questions asking how to export a file from Chrome but I have not found a good answer.

I have written a Chrome Application (Legacy Packaged Application) in which I am getting some data for the user. I just need to create some text in a Chrome window that the user can save to a file. The text would be tab-delimited text. If I use code such as what I show below then Chrome will not save the data; the Save As command is either disabled or does nothing. If we try to view the source (for diagnostic purposes at least) then Chrome crashes. Is there a way to create a window with text in it that can be saved? Or if there is a way to export a file then that would be better.

I have seen the filesystem API but as best as I understand it, it must either execute in a server or requires Chrome to be started with a special option. That makes the filesystem API too impractical.

I realize that a possible alternative is to create XML instead of plain text. I will offer XML as an alternative but I also want to provide plain text data.

This is a sample of the code I am using to create a window with text in it:

var w = window.open("BlankPage.html", "_blank", "menubar=yes", false);
var d = w.document;
d.open('text/plain', 'replace');
d.charset = "utf-8";
d.write("Hello");
d.close();
Was it helpful?

Solution

I use Filesaver.js in my Chrome extension to download a backup of the archive as a txt file.

Quoting from its README file:

FileSaver.js is the solution to saving files on the client-side, and is perfect for webapps that need to generate files, or for saving sensitive information that shouldn't be sent to an external server.

FileSaver.js GitHub: https://github.com/eligrey/FileSaver.js/

Demo: http://eligrey.com/demos/FileSaver.js/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top