Thursday, April 12, 2012

Can javascript access a filesystem?


I was pretty sure the answer was NO , and hence google gears, adobe AIR, etc.



If I was right, then how does http://tiddlywiki.com work? It is persistent and written in javascript. It is also just a single HTML file that has no external (serverside) dependencies. WTF? Where/how does it store its state?


Source: Tips4all

7 comments:

  1. Tiddlywiki has several methods of saving data, depending on which browser is used.


    If ActiveX is enabled, it uses Scripting.FileSystemObject.
    On Gecko-based browsers, it tries to use UniversalXPConnect.
    If Java is enabled, it uses the TiddlySaver Java applet.
    If Java LiveConnect is enabled, it tries to use Java's file classes.

    ReplyDelete
  2. HTML5's File[1], FileWriter[2], and FileSystem[3] APIs are available in the latest Developer channel of Google Chrome. The FileSystem API lets you read/write to a sandbox filesystem within a space the browser knows about. You cannot, for example, open 'My Pictures' folder on the user's local FS and read/write to that. That's something in the works, but it won't be ready for a while. Example of writing a file:

    window.requestFileSystem(
    TEMPORARY, // persistent vs. temporary storage
    1024 * 1024, // 1MB. Size (bytes) of needed space
    initFs, // success callback
    opt_errorHandler // opt. error callback, denial of access
    );

    function initFs(fs) {
    fs.root.getFile('logFile.txt', {create: true}, function(fileEntry) {

    fileEntry.createWriter(function(writer) { // FileWriter

    writer.onwrite = function(e) {
    console.log('Write completed.');
    };

    writer.onerror = function(e) {
    console.log('Write failed: ' + e.toString());
    };

    var bb = new BlobBuilder();
    bb.append('Lorem ipsum');
    writer.write(bb.getBlob('text/plain'));

    }, errorHandler);
    }
    }


    Check out this HTML5 Storage slide deck for more code snippets.

    ReplyDelete
  3. It uses a java file references like this:

    drivers.tiddlySaver = {
    name: "tiddlySaver",
    deferredInit: function() {
    if(!document.applets["TiddlySaver"] && !$.browser.mozilla && !$.browser.msie && document.location.toString().substr(0,5) == "file:") {
    $(document.body).append("<applet style='position:absolute;left:-1px' name='TiddlySaver' code='TiddlySaver.class' archive='TiddlySaver.jar' width='1'height='1'></applet>");
    }
    },
    isAvailable: function() {
    return !!document.applets["TiddlySaver"];
    },
    loadFile: function(filePath) {
    var r;
    try {
    if(document.applets["TiddlySaver"]) {
    r = document.applets["TiddlySaver"].loadFile(javaUrlToFilename(filePath),"UTF-8");
    return (r === undefined || r === null) ? null : String(r);
    }
    } catch(ex) {
    }
    return null;
    },
    saveFile: function(filePath,content) {
    try {
    if(document.applets["TiddlySaver"])
    return document.applets["TiddlySaver"].saveFile(javaUrlToFilename(filePath),"UTF-8",content);
    } catch(ex) {
    }
    return null;
    }
    }

    ReplyDelete
  4. The answer is in the future, it will. See http://www.w3.org/TR/FileAPI/.
    With Firefox 3.6.8 see http://www.html5rocks.com/tutorials/file/dndfiles/ for samples with source code.

    ReplyDelete
  5. Technically you can do

    netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserWrite');


    in a netscape-compatible browser (Firefox, Mozilla, Netscape), and it will ask the user* whether or not to allow filesystem access, but this is not portable.

    *once per browser process

    ReplyDelete
  6. The answer is indeed NO. Java applets, and the dreaded ActiveX plugins are usually used if this is required

    ReplyDelete
  7. Can javascript access a filesystem?


    Not outside of the sandbox area mentioned above, to the best of my knowledge. However, it can access a signed java applet that has callable public methods which can get to all files. I have done it and it works fine and is cross browser.

    The signing part is somewhat involved and for professional use you might need to pay for a code signing certificate which authorises your identity. Get it from some place like Verisign. That way users at least know who the applet is written by (if that helps). You can sign it yourself for free but one of those "possible security risk" popups will occur at first use for authorisation by the user.

    You would think that such signed applets for file writing would exist already for download but I couldn't find any via searching. If they did, you could just plug it in your page, learn the API and off you go.

    ReplyDelete