Page created:
Jun 16, 2009 (? ago)
Last modified ? ago
To main page | Opera Unite HowTo's

fileio: Sandboxed Filesystem


File Sandbox

The 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/saving

A 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 library

Change 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 file

This 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 file

var stream = dir.open('/storage/newfile', opera.io.filemode.WRITE);
stream.write(counter);
stream.close();

Sources

Example 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.

JSON

Ideally we need to save JSON representation of object, but I haven't discovered a simple way to use JSON under Opera Unite.

See also

http://dev.opera.com/libraries/fileio/

Slava V. [about me]


main page



Last updated


  1. Markuper (HTML templates)
  2. .ua
  3. Opera Unite HowTo's
  4. .us (files)
  5. Distribute Your Application
  6. 24/7 sites (permanent applications - idea) [stub]
  7. Issues
  8. HTTP Connections (AJAX/REST)
  9. Cookies
  10. file_wrap.js - File Wrapper
  11. Basic HowTo: Simple app (tutorial)
  12. Static images, client-side scripts
  13. Application Examples
  14. How to Debug Opera Unite apps
  15. Persistence & databases
  16. Opera Unite benchmark
  17. From PHP to Opera Unite
  18. Uniteness (Framework)
  19. Config.xml
  20. Key-value storage
  21. Widget Object
  22. Notifications (Growl'esque)
  23. Reset (debug)
  24. Cron example
  25. What I meant by CNAMEs
  26. Wish List
  27. Device Unavailable
  28. StopLorem (Opera Unite blogging)
  29. uniteness-0.11
  30. GET/POST data
  31. CRUD And Static (example)
  32. Opera object
  33. URLs
  34. Headers & Redirects
  35. Error Console
  36. JSON State (storing data)
  37. Security
  38. /storage/ (in fileio)
  39. Yusef library
  40. unite_info (a-la php_info)
  41. Javascript Imports
  42. onunload / _close
  43. fileio: Sandboxed Filesystem
  44. Request Hierarchy (like php_info)
  45. Intro: Web Apps with Opera Unite