To main page | Opera Unite HowTo'sfileio: Sandboxed FilesystemFile SandboxThe filesystem used by Opera Unite is located deep in the /Documents and Settings/ folder, so there is no real way to load file with absolute path (i.e. get out of sandbox and do something nasty). For futher explanation - see fileio docs →.In short - you can only access the storage folder (where your app creates files) or folder where your app resides (in read-only mode) or folder explicitly specified by user. File reading/savingA full example is file_wrap.js.Here's an example of how to Load/Save a simple integer var: 1. Start with Basic HowTo, then: Enabling fileio libraryChange config.xml, add this:(right above <feature name="http://xmlns.opera.com/webserver">): <feature name="http://xmlns.opera.com/fileio"> </feature>This enabled Opera Unite to use filesystem. How to load from fileThis line is common for both reading and writing:var dir = opera.io.filesystem.mountSystemDirectory('storage');Then: try { stream = dir.open(dir.resolve('/storage/newfile'), opera.io.filemode.READ); if (stream) { var counter = parseInt(stream.readLine()); stream.close(); }; } catch(err) { // message: FILE_NOT_FOUND_ERR var counter = 0; };Warning This reads only one line. To read whole file, see file_wrap.js Opera says → there's a method .exists(), that could be used to check whether file exists, but I haven't found it. (Tried all combinations I can think of) dir.open raises FILE_NOT_FOUND_ERR Exception if it haven't found file. These lines above are loading integer from a file /storage/newfile/. It's not an absolute path, the read path would be smth. like: /Documents and settings/..../Local settings/..../4393408934/storage/newfile (this is why it's Sandbox - you can't go to just any arbitrary file, unless you use shared folder) How to save to filevar stream = dir.open('/storage/newfile', opera.io.filemode.WRITE); stream.write(counter); stream.close(); SourcesExample counter source →This source is suboptimal, due to the fact that it saves each time a request is made, but I haven't found a way to save only when Opera/service closes. There's no onunload, but there's '_close event' that does the same. The code for saving the state should go there. JSONIdeally we need to save JSON representation of object, but I haven't discovered a simple way to use JSON under Opera Unite.See alsohttp://dev.opera.com/libraries/fileio/ → | Last updated
|