Skip to main content

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

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

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex