Node-Webkit: How to create a file in the local file system?

cohoman picture cohoman · Aug 7, 2013 · Viewed 11.8k times · Source

With using Node-Webkit, I'm trying to create and write to a text file in a user defined local folder from within a web worker js file. I tried using requestFileSystemSync(), .root.getFile(), .createWriter(), etc. but I'm not sure where this file is saved (can it be written to an defined local folder, or is it "sandboxed" to a special folder location?).

Can anyone offer advice on the best way for me to create text files from a web worker to an arbitrary local folder location? That is, in the local file system outside of a sandboxed file system?

Thanks.

Answer

Nathan Breit picture Nathan Breit · Aug 7, 2013

It sounds like you might be trying to use the web filesystem API. That might be a reasonable solution, it would allow you to run your app as a regular webpage, however the web filesystem API is sandboxed.

node-webkit offers "Complete support for Node.js APIs," which I think means you can use the node.js file system API to write anywhere on the file system. Pretty cool. So something like this should work:

var fs = require('fs');
fs.writeFile("path/to/anywhere/test.txt", "Hi mom!", function(err) {
    if(err) {
        alert("error");
    }
});

Also, node-webkit web-workers do not support require I hear. So you'll need to do your filesystem stuff in your main thread.