4

I'm using the HTML5 FileSystem API in a Chrome Packaged App to write to a log file. I want the user to be able to download this file, so I tried something along the lines of:

fs.root.getFile('log.txt', {create: false}, function(fileEntry) {
  var url = fileEntry.toURL();
  // do something with the file url
});

This doesn't help though, because the URL is something like filesystem:chrome-extension://eekedjcagggbfigdmifkmhkjbhiklnpj/temporary/log.txt and it's not possible to open it somewhere.

What technique would you recommend to make a FileSystem API file in a Packaged App downloadable?

Edit: After reading through Ben Well's answer below, I realized that I'll have to clarify even more what I want. It would seem especially nice to me if there would be a technique that doesn't imply loading the HTML5 Filesystem API file contents, building a blob from it and writing that to a user-chosen path with chrome.fileSystem API.

Community
  • 1
  • 1
bfncs
  • 10,007
  • 4
  • 32
  • 52
  • Do you know that the intent of packaged apps is to be different than web apps, right? With that in mind I think what you want is to have some kind of "This site is trying to send you something, do you like to keep it?", what is a web app communicating with web server "thing". If you embrace the native app way of thinking the most common mechanisms you'll have are: save the file somewhere, render the content on your screen or copy it to the clipboard. Help us help you, what you are willing to do and what are you able to give up in order to achieve that? Thanks – Jayr Motta May 22 '13 at 14:15
  • I'm pretty much aware of why I want to do it as a packaged app as opposed to building a web app. I'm just looking for the most convenient way in API terms to save information via HTML5 Filesystem API (not requiring the user to interact for this in the first place) and make it available to save for the user - ideally sparing the additional step of building a blob object of the file. – bfncs May 28 '13 at 05:07

1 Answers1

4

Have you tried using chrome.fileSystem.chooseEntry? This API lets your app save files to the user's hard disk, wherever they want, letting your program have a Save As kind of command. This is a bit different to a download link, but is also more in harmony with V2 apps being like native apps.

chrome.fileSystem pops up a dialog asking the user to choose a location for a file. If you use the options for saving files, this returns you a file entry that has permissions to write to the location chosen by the user. The user can also create new files when you use these options.

Ben Wells
  • 1,896
  • 13
  • 9
  • 1
    Thanks a lot for your quick answer. I'm aware of chrome.fileSystem but what I want to do is to persist a file with the sandboxed HTML5 FileSytem API ("out of sight" for the user) but offer a possibility to export it to a path of this choice. The route you suggest would imply getting the file contents from HTML5 Storage, creating a new Blob with it and saving it to a user-specified path with chrome.fileSytem.chooseEntry. While this should definitely work, I was hoping for a simpler technique that doesn't need to load the file contents. I'm already upvoting, but will wait for other answers, too. – bfncs May 08 '13 at 08:54
  • "what I want to do is to persist a file with the sandboxed HTML5 FileSytem API ("out of sight" for the user)", that could be in the persistent area? "but offer a possibility to export it to a path of this choice", this is exactly what Ben Wells suggested? – Jayr Motta May 22 '13 at 14:37
  • I had a similar problem and ended up using this solution. It's rather slow for large files but works OK. I think it's a security issue -- allowing the file to be downloaded without the user selecting the exact path first manually leaves too much room for attacks. – kzahel Aug 16 '13 at 14:26