File I/O API

By Opera Software

This document is the API Specification for File I/O API.



This API document describes the JavaScript bindings for accessing the local file system in Opera.

The basics

The File I/O API consists of three classes: FileSystem, File and FileStream.

The FileSystem class is initialized as a singleton, and is available as This is a virtual file system. In order to actually use it, you'll need to add directories from your actual file system as mount points to the virtual filesystem.

The File object works like similar objects in other frameworks. It can point to a directory, archive or regular file. It exposes properties like path, isDirectory, exists, parent, etc. It also works as an array to let you access files and subdirectories in a directory.

The FileStream is used when you want to read from or write to the files in the filesystem. It supports reading and writing text, images, binary data and Base64 text.

Enabling File I/O

In order to make the file system and its methods available, you need to add a feature element to your config.xml file like this:

  <feature name="">
    <param name="folderhint" value="home" />

The folderhint parameter is used in conjunction with the shared folder. If the parameter is present, the user will be presented with a file dialogue that defaults to a directory corresponding to the value of the parameter. If multiple folderhint parameters are present, and the implementation supports only one shared folder, the last is used. If the parameter values do not correspond to a directory, the system will use its default starting location for the file dialogue.

If no folderhint parameter is present, no dialogue will be presented to the user and no shared folder will be available.

The following folder hints are recognized:

The user's default home directory, or other appropriate directory (My documents on Windows, /home/username/ on Linux, /Users/username/ on Mac)
The user's default pictures directory
The user's default music directory, such as /home/username/Documents/My Videos on Ubuntu)
The user's default video directory
The user's default documents directory ( such as /home/username/Documents on Ubuntu)
If the user has a default downloads directory
The desktop, where applicable.

You may compress multiple param elements to value="home music pictures".

The following method is deprecated: In order to make the file system and its methods available, you need to add a file attribute with the value yes to the widget element in the config.xml file of your widget or service.

Mount points

Instead of accessing the file system directly, this API uses a concept of mount points. In order to access parts of a disk, it must first be mounted as a mount point in a virtual filesystem. There are two types of mount points:

System mountpoints: the application, storage and shared directories

There are three special directories you can use with the File I/O API:

  • The application directory, which contains the actual files in the application accessing the file system. If the application is a widget, this directory will contain all the files in the widget, like config.xml, index.html and others. This directory is always mounted as readonly.
  • The private storage directory, which can be used to save temporary files and configuration information specific to the application. The files stored in this directory persist until the application is uninstalled.
  • The private shared directory, which can be used to share and store files. This directory is typically chosen by the user when installing the application, and is accessible to the user in the normal file system. One example of use is sharing a set of images from somewhere on the user's disk.

These are not available by default and need to be mounted using the method:'shared');'storage','myCoolSaves');

Once mounted, they become available under in the property.

You may specify an optional name to mount these directories as. If not supplied, it defaults to application, storage and shared respectively.

In the example above shared directory will be mounted as shared and have a path /shared, while storage will be mounted as myCoolSaves and have a path /myCoolSaves.

These can then be accessed as regular mount points and through the mountpoint URL protocol as other mounted files, except that the application directory is mounted as read-only.

WARNING: The shared directory will be read-write, unless the underlying file system defines it to be read-only. Be careful to protect your data by controlling how data gets written to it. You should supply some sort of authentication of users who access data in this folder and be careful to not leave code open to exploitation.

Creating your own mount points

Note: Creating mount points with the browseFor* methods is not supported in Opera Unite services. It should be possible for Opera Widgets. Mounting system mount points should work in both cases.

It is possible to create your own mount points from any directory on the user's disk, using the, and methods.

These functions will open a file dialog, and let the user choose a file to share. The selected file is returned as an argument to a callback function. If the user cancels the dialog, or the selected file is somehow invalid, the callback function is called with null.

WARNING: Once mounted, the mount point will be read-write unless the underlying file system defines it to be read-only. Be careful to protect your data by controlling how data gets written to them. You should supply some sort of authentication of users who access these directories and be careful to not leave code open to exploitation.

The following is an example using browseForDirectory(), which is the most common case: 'share', '', processDir ); //Let the user choose a directory
function processDir( dir )
    if ( ! dir )
        return; //Invalid file or canceled dialog

In this case, 'share'; becomes the name of the directory in the virtual file system. The processDir function is called with the file the user selects.

Mount points become available in the property. This object is a File object.

The mountpoint URL protocol

In some cases, you want your application to be able to reference files that have been mounted in the virtual file system from a Web page. You can use the mountpoint URL protocol for this purpose. A mountpoint URL starts with mountpoint://, followed by the name of a mount point and a path to a file under that mount point.

For example, if a user has added a mount point, and named it myImages using the call:


the user can access files inside the mount point by creating an absolute URI:

<img src="mountpoint://myImages/avatar.png">


Note that the path separator is always '/', regardless of the underlying file system.

The fileSystem.mountPoints property represents the root of the file system and has the path '/'.

A mount point mounted with the name foo has the path '/foo'.

All files belong to only one mount point, so if a directory mounted as 'foo' has a file called 'bar', the path of the file is '/foo/bar'.

Paths that begin with '/' are absolute paths, starting from the root and moving down through a mount point, through any subdirectories and potentially to a file.

You may use relative paths. Any path not starting with a '/' is a relative path. The '.' and '..' directory references are supported. Paths are relative to the current directory. If file is a regular file, and you call file.moveTo('some/path') or similar methods, the path is relative to the parent directory of file. If file is a directory, the path is relative to that directory.

Working with files

You obtain an initial file by adding a mount point as described earlier. From here you have two options:

If the mount point is a directory, you can move into its content as described in the next section.

You can use the resolve() method on the initial File object to get a reference to a File somewhere under the mount point. This method takes a path as an argument and will attempt to resolve it. If the path is valid, an File object is returned. Note that the file does not need to exist; the path simply needs to be a possible valid path.

var file = mp.resolve('path/to/my/file');

Note that the path separator is always '/', regardless of the underlying file system.

Some important properties of the File object:

Check if the file referenced by this File object actually exists. Especially useful when using the resolve() method.
If the File object references a regular file.
If the File object references a directory.
When the file was created.
When the file was last modified.
The path to this file in the virtual file system, starting with '/' and the name of the mount point.

You may copy and move files by using the copy and moveTo methods:


Both methods take an optional argument overwrite, which will cause any existing files with the new path to be overwritten.

To create a new directory, use the following syntax:

var file = mountPoint.createDirectory(somePath);
var file = mountPoint.createDirectory(mountPoint.resolve(somePath));

In order to write files, you need to open a FileStream to the file and write to it. See the section on working with streams.

To delete files or directories, use the deleteFile() or deleteDirectory() methods:

mp.deleteDirectory('path/to/directory', true);

Both methods may take a File object instead of a path. The second argument is to delete content recursively

Working with directories

A File object made from a directory points to its subdirectories and contained files. The File object supports a length property and an array-like syntax to access these subfiles. Note that the subfiles and directories need to be 'refreshed' before you can actually access them. Through this process, information about the files in the directory are loaded into the virtual filesystem. The method refresh() is used for this purpose:

dir.refresh(); //Load the contents of the directory
for ( var i = 0, file; file = dir[i]; i++ )
    opera.postError(file.path + ' ' + file.isDirectory + ' ' file.isFile);

When the file is a directory, its length property will tell you how many files and subdirectories there are in the directory.

It's important to note that information about the subfiles and directories of this directory is not live. If files are added, moved or deleted, you need to call refresh() again to update the information in the File object.

You can similarly recurse through the file structure.

Reading and writing: Working with files streams

In order to read or write to a file, you need to make a File object and then open it for reading or writing using the open method:

var stream ='newfile',;
stream ='newfile');
var data = stream.readLine();

Using will overwrite all data in the file. Use to append data instead. If the file does not exist, it is immediately created when opened in either of these modes.

The following modes are supported:

Open the file for reading. If the file doesn't exist, throw an exception.
Open the file for writing. This will delete everything in the file first. If the file doesn't exist, it is created.
Open the file appending. This will write data at the end of the file. If the file doesn't exist, it is created.
Open the file for reading and writing. If the file doesn't exist, throw an exception.

The modes can be combined using a bitwise OR: ( READ | WRITE ).

You may write characters, lines, Base64-encoded text and images to the stream, using the different writeX() methods of the FileStream object.

This article is licensed under a Creative Commons Attribution, Non Commercial - No Derivs 2.5 license.


The forum archive of this article is still available on My Opera.

  • photo


    Friday, January 27, 2012

    what is "mp" in code example? Where are we get it?
  • photo

    Chris Mills

    Friday, January 27, 2012

    @Victor - sorry that this isn't more clearly written. I think "mp" is just the mount point stored in a variable. You can find more information at the Github page where the library is stored:
  • photo

    Mikhail Niedre

    Wednesday, February 15, 2012

    @Chris, Can I use html5 File API instead Opera File I/O API ?
  • photo

    Chris Mills

    Sunday, February 19, 2012

    @Mikhail - yes, you should be able to. And the file API is a more standardised solution for basically the same problem. I think I need to add an obsolete message to this page soon.
  • photo

    Edward Simpson

    Thursday, March 1, 2012

    1. Do Opera Mobile 10 Widgets on Windows Mobile 6.5 support File I/O or the HTML5 File API yet?
    2. If so, is there an example of the above?
  • photo

    Chris Mills

    Friday, March 2, 2012

    @Edward Simpson

    No, they don't support those unfortunately. I'll try to find out if we have any plans in that area.
  • photo

    Mikhail Niedre

    Thursday, March 8, 2012

    HTML5 File API also do not supported correctly in the Opera desktops Widgets at this moment. :-(
No new comments accepted.