17

I'm playing around a bit with the FileSystem API.

I've found a lot of examples where you generate a download link and let the user download the file the "browser way".

I would like to know two things:

  1. Is there any way to write the ajax result in the fiddle as a file directly to the disk (without any type of prompt). Like to the user's desktop for example.

  2. Is blob the most suitable format for this?

http://jsfiddle.net/FBGDe/

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
    if (this.readyState == 4 && this.status == 200){
        console.log(this.response, typeof this.response);
        var img = document.getElementById('img');
        var url = window.URL = window.webkitURL;
        img.src = url.createObjectURL(this.response);
    }
}
xhr.open('GET', 'http://www.newyorker.com/online/blogs/photobooth
                                                       /NASAEarth-01.jpg');
xhr.responseType = 'blob';
xhr.send();      
Johan
  • 35,120
  • 54
  • 178
  • 293
  • 1
    "Without any prompt". That would be a serious security issue. – 11684 Nov 02 '13 at 16:41
  • 3
    you can use http://danml.com/js/download.js to save a named file to the downloads folder, without prompt. if you try to download a bunch of files, chrome will ask for approval once per session. – dandavis Nov 02 '13 at 18:49
  • @11684 I think that you're able to delete files without a prompt with this API. I'd say that's an even bigger security issue in this context – Johan Nov 02 '13 at 20:19
  • @dandavis Thanks, I'll look in to that – Johan Nov 02 '13 at 20:19
  • @Johan, no, because with this API you can only delete files your website put there in the first place. – 11684 Nov 03 '13 at 12:02
  • Related post - [HTML5 offline storage. File storage? Directories and filesystem API](https://stackoverflow.com/q/3936736/465053) – RBT May 22 '19 at 09:54

2 Answers2

14

Please note that Filesystem API is no longer part of the standard's specification, as specified at: http://www.w3.org/TR/file-system-api/

EDIT: Quoting the specification in case the link changes: "File API: Directories and System W3C Working Group Note 24 April 2014

Work on this document has been discontinued and it should not be referenced or used as a basis for implementation."

(This does not relate to the question directly, but it is essential to know not to use the FileSystem API further.)

Another link: http://www.html5rocks.com/en/tutorials/file/filesystem/

"In April 2014, it was announced on public-webapps that the Filesystem API spec should be considered dead. Other browsers have showed little interest in implementing it."

Aparajith Sairam
  • 373
  • 5
  • 14
  • Ok, I will add details, not just the link, henceforth. Thanks. – Aparajith Sairam Apr 29 '14 at 16:15
  • 2
    Exactly same [end for Web SQL Databases](https://softwareengineering.stackexchange.com/q/220254/236257) as well which got deprecated. For offline web applications, I believe [Indexed DB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB) and [web storage](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API) are the remaining options then. – RBT Oct 25 '17 at 02:31
9

Unfortunately, writing to regular files is not currently possible (despite the accepted answer Modifying Local Files Using HTML5 and JavaScript).

You can only write to the sandboxed filesystem.

FYI, you can do this in a Chrome Packaged App: http://developer.chrome.com/apps/fileSystem.html But even then the user must at least choose the file first. Writing to any file would be a serious security hole.

What problem are you really trying to solve?

Community
  • 1
  • 1
Paul Draper
  • 78,542
  • 46
  • 206
  • 285
  • 1
  • Thank you and sorry for a late comment. I would like the user to mark a bunch of files > choose a folder > press download and let the browser download them simultaneously. But I guess it's easier to just zip them serverside and download it the regular way. – Johan Nov 02 '13 at 20:17
  • Just to elaborate, I don't want to write to an existing file, I want to create a new file. But I suppose that's what you are referring to? – Johan Nov 02 '13 at 20:21
  • @Johan, you will have to zip. And the same restrictions apply whether new files or existing files. – Paul Draper Nov 03 '13 at 00:26
  • 1
    @PaulDraper, this is an old topic, but the problem I would be trying to solve here is a JavaScript based app, which needs to create dummy SQL data on the client side. Say, a CSv file with 10000 dummy names. To save the user from downloading a big file over the internet, I am trying to get it created at the client side via javascript. – Saurabh Kumar Apr 28 '15 at 21:31