Lesson 3: Working with the FileSystem API CHAPTER 16 601
After this lesson, you will be able to:
■■Describe the use of the FileSystem API.
■■Implement the FIleSystem API.
Estimated lesson time: 30 minutes
Assessing browser support
As of this writing, the only major browser supporting the FileSystem API is Chrome, so you
might want to have it installed if you want to follow along with the examples. It is not known
if other browsers are planning to add support in the future.
Because of its limited browser support, the most common applications to take advantage
of the FileSystem API are those targeting Chrome, such as Chrome extensions.
Opening the file system
The JavaScript window object contains a method for making requests to open communica-
tion with the file system. Depending on the version of Chrome you have installed, this can be
either window.requestFileSystem() or window.webkitRequestFileSystem(). To make the follow-
ing examples as compatible as possible, the following snippet is at the top of the subsequent
scripts.
window.requestFileSystem = window.requestFileSystem ||
window.webkitRequestFileSystem;
The requestFileSystem method operates asynchronously and includes success and error
callbacks that provide notification that the application can begin reading and writing files.
The following parameters are accepted.
■■type his parameter specifies whether the file system should be temporary or perT -
manent. If temporary, you pass TEMPORARY as the argument, and the browser auto-
matically removes the files as it needs space. If you pass PERSISTENT, files are removed
only when requested by the application or user.
■■size This parameter is an estimation of the size (in bytes) that the application will
need.
■■successCallback he request for a file system is successful, this callback is invoked If t
and includes a FileSystem object as an argument.
■■errorCallback If the request fails, this callback is invoked and includes a FileError
argument. The error includes a code that indicates why the failure occurred, such as
exceeding quota or discovering security issues.