606 CHAPTER 16 Offline web applications
function directoryOpened(directoryEntry) {
directoryEntry.remove(directoryRemoved, handleError);
}
function directoryRemoved() {
alert('Success');
}
function handleError(error) {
alert(error.code);
}
To delete a directory forcefully, along with any contents under it in subdirectories and files,
use the removeRecursively method as follows.
function directoryOpened(directoryEntry) {
directoryEntry.removeRecursively(directoryRemoved, handleError);
}
Lesson summary
■■You can use the File System API to store binary files on the client’s file system.
■■Chrome is currently the only major browser supporting File System API.
■■Communication with the file system is initiated through the requestFileSystem method
available on the window object.
■■File systems can be created by using two modes. The first, TEMPORARY, allows the
browser to remove the files automatically if space is needed. Only the application or
user can delete contents if the second mode, PERSISTENT, is used.
■■Files are created and opened by using the getFile method on the DirectoryEntry
object.
■■A FileWriter is used to write and append to a file.
■■The FileReader class contains the following methods for reading the contents of a file:
readAsText, readArrayBuffer, and readAsDataURL.
■■A file can be removed by calling the FileEntry remove method.
Lesson review
Answer the following questions to test your knowledge of the information in this lesson. You
can find the answers to these questions and explanations of why each answer choice is correct
or incorrect in the “Answers” section at the end of this chapter.
- Which of the following methods should you use when storing files that are relatively
unimportant and can be removed if the browser is low in available space?
A. window.requestFileSystem(LOW, 5 1024 1024, getDirectory, handleError);
B. window.requestFileSystem(PERSISTENT, 5 1024 1024, getDirectory, handleError);