0

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();
Sam Hobbs
  • 2,594
  • 3
  • 21
  • 32
  • http://davidwalsh.name/downloadify – SLaks May 11 '14 at 21:19
  • Do you mean https://github.com/dcneiner/Downloadify? It says "for online use only". – Sam Hobbs May 11 '14 at 21:48
  • That just means you can't run it from `file://` – SLaks May 11 '14 at 21:52
  • I think it does not **just** mean not from file://. Regardless, it must work for a Chrome Application. Are you sure Downloadify works in a Chrome Application? Unless you know otherwise, "for online use only" means it won't. – Sam Hobbs May 11 '14 at 22:16
  • possible duplicate of [Making a Chrome Extension download a file](http://stackoverflow.com/questions/4845215/making-a-chrome-extension-download-a-file) – SLaks May 11 '14 at 22:42
  • Totally different. In that, the file is being downloaded from the server ("will download a mp3 file **off a website**"). – Sam Hobbs May 11 '14 at 22:55
  • Read the answer; in that question, the OP already has the data locally. That answer is what you're looking for. – SLaks May 11 '14 at 22:59
  • That other question is misleading since the original question says "will download a mp3 file off a website". It makes it difficult to find. Assuming that will work for large files, then yes, that looks promising. I wish **my** question was better understood initially, the part where I said Chrome Application. – Sam Hobbs May 11 '14 at 23:17
  • If the other answer requires additional permissions then I cannot use it since my application must be a legacy packaged application; it will not work as a non-legacy packaged application. I can get the blob to work outside my app but not in my app. – Sam Hobbs May 12 '14 at 00:50
  • One of the answers to a [similar question](http://stackoverflow.com/questions/7338640/document-opentext-plain-formatting-ignored-in-webkit-safari-chrome) is to prefix the document content with the long-ago-obsoleted tag (still supported by at least Chrome in 2016).</plaintext></span> –&nbsp;<a href="../../users/1081957/synexis" title="1,255 reputation" class="comment-user ">Synexis</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/23598018/creating-a-plain-text-document-window-in-chrome#comment57608735_23598018"><span title="2016-01-22T02:54:27.450 License: CC BY-SA 3.0" class="relativetime-clean">Jan 22 '16 at 02:54</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <div id="answers"> <a name="tab-top"></a> <div id="answers-header"> <div class="answers-subheader grid ai-center mb8"> <div class="grid--cell fl1"> <h2 class="mb0" data-answercount="9">1 Answers<span style="display:none;" itemprop="answerCount">1</span></h2> </div> </div> </div> <a name="23607147"></a> <div id="answer-23607147" class="answer accepted-answer" data-answerid="23607147" data-ownerid="3152667" data-score="0" itemprop="acceptedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="23607147"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="0">0</div> <div class="js-accepted-answer-indicator grid--cell fc-green-500 py6 mtn8"><div class="ta-center"><svg aria-hidden="true" class="svg-icon iconCheckmarkLg" width="36" height="36" viewBox="0 0 36 36"><path d="m6 14 8 8L30 6v8L14 30l-8-8v-8z"></path></svg></div></div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>I use <strong>Filesaver.js</strong> in my Chrome extension to download a backup of the archive as a txt file.</p> <p>Quoting from its README file:</p> <blockquote> <p>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.</p> </blockquote> <p>FileSaver.js GitHub: <a class="external-link" href="https://github.com/eligrey/FileSaver.js/" rel="nofollow">https://github.com/eligrey/FileSaver.js/</a></p> <p>Demo: <a class="external-link" href="http://eligrey.com/demos/FileSaver.js/" rel="nofollow">http://eligrey.com/demos/FileSaver.js/</a></p></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered May 12 '14 at 10:57">answered May 12 '14 at 10:57</time> <a href="../../users/3152667/mayank-agarwal" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/3152667.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Mayank Agarwal" /> </a> <div class="s-user-card--info"> <a href="../../users/3152667/mayank-agarwal" class="s-user-card--link">Mayank Agarwal</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">81</li> <li class="s-award-bling s-award-bling__silver" title="1 silver badges">1</li> <li class="s-award-bling s-award-bling__bronze" title="6 bronze badges">6</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-23607147" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="23607147" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-36419988" class="comment js-comment " data-comment-id="36419988" data-comment-owner-id="2392247" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment36419988_23607147"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Thank you. It is wonderful when something works. This is the first time I have gotten something useful from a question I asked in Stackoverflow.</span> –&nbsp;<a href="../../users/2392247/sam-hobbs" title="2,594 reputation" class="comment-user owner">Sam Hobbs</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/23598018/creating-a-plain-text-document-window-in-chrome#comment36419988_23607147"><span title="2014-05-16T17:16:39.923 License: CC BY-SA 3.0" class="relativetime-clean">May 16 '14 at 17:16</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> </div> </div> </div> </div> <script src="../../static/js/stack-icons.js"></script> <script src="../../static/js/fromnow.js"></script> </body> </html>