Chapter 12
Files
WHAT YOU WILL LEARN IN THIS CHAPTER:
- Learning about filesystems
- Learning about directories and files
- Learning how to transfer files
- Handling errors
In Chapter 11, you learned how to work with storage options — both local storage and databases. In this chapter, you learn how to work with filesystems, directories, and files. By the end of this chapter, you’ll know how to access the filesystem, read and write files, and navigate directories. You’ll also be able to transfer files from the device to a remote server.
LEARNING ABOUT FILESYSTEMS
Regardless of manufacturer or operating system, every smartphone or device has a filesystem, and the PhoneGap API provides some access to that filesystem.
At the end of the day, that access is usually fairly limited. For example, it’s difficult (if not impossible) to jump outside the sandbox your application lives in. However, let’s assume that you’re not here to learn about jailbreaking your phone’s 16 GB (for example) memory capacity.
What this chapter focuses on is what probably matters most to you: how to access the filesystem so that you can read a file that’s already there, or write some data to a file.
PhoneGap’s FileSystem object represents information about the filesystem. It has the following two properties:
- name — This is the name of the filesystem.
- root — This is the root directory of the filesystem.
This object is returned as the success callback of the requestFileSystem() method. Following is an example:
window.requestFileSystem(LocalFileSystem.PERSISTENT, ...