0

I have read the nice article http://www.html5rocks.com/en/tutorials/file/xhr2/

It uses HTML5 FileSystem API and FileWriter to achieve writing Blob to local files.

However, my browser Safari (currently 7.0.5) doesn't support requestFileSystem and FileWriter. (I have tested with Chrome. It doesn't support either.)

I also searched online but there didn't seem something like "formal answers".

Another question is why reading local files to browser is simple (using FileReader), but writing to local files doesn't seem symmetric, especially in the new html5 standard.

  • 1
    Writing to local files poses more of a potential security risk, and it has a newer API besides. However, forcing downloads has always worked. – rvighne Jul 20 '14 at 02:53

1 Answers1

2

In webkit (Chrome and Safari), you can in fact use the FileSystem api via a vendor prefix:

window.webkitRequestFileSystem

And FileWriter isn't directly exposed to JavaScript anyway.

For non-supporting browsers (which is many): https://github.com/eligrey/FileSaver.js will help.


Have you considered forcing a download the old-fashioned way (more compatibility)? Read the Blob as a Data URI, then change the content-type of the URI to a value that forces a download. Downside: no specifying the file name and tedious.

var reader = new FileReader;
reader.onload = function() {
    window.open(this.result.replace(/data:.+?\/[^;]+/, "data:application/octet-stream"));
};
reader.readAsDataURL(my_blob);
rvighne
  • 20,755
  • 11
  • 51
  • 73
  • I tried [FileSaver.js](https://github.com/eligrey/FileSaver.js) and it didn't show the dialog like `` does. Do I misunderstand anything? Or is it platform-dependent? (Mine is Mac OS/Safari) – RedBeanPieLarry Jul 20 '14 at 06:01
  • Btw, I couldn't find the output file in the local file system either. – RedBeanPieLarry Jul 20 '14 at 06:18
  • @RedBeanPieLarry: The file would be in your downloads folder (or depending on your browser's settings, you'd be prompted where to save it). And there's no way to show a file saving dialog lile that unless your browser *natively* supports the API or its settings cause it to ask you where to save downloads. – rvighne Jul 20 '14 at 16:12