Can javascript access a filesystem? [duplicate]
Tiddlywiki has several methods of saving data, depending on which browser is used. As you could see in the source.
- 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.
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.
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; } }