← Plugins / File System
This official Apache Cordova plugin lets your app read and write files inside its own sandboxed storage. It exposes an API modelled on the HTML5 FileSystem spec, giving you directory and file entries, readers and writers to move data on and off the device's disk.
Add the plugin to your Cordova project from the command line:
cordova plugin add cordova-plugin-file
Two global functions get you a handle on the filesystem. Call them after the deviceready event has fired:
| Function | Description |
|---|---|
window.resolveLocalFileSystemURL(url, success, fail) | Resolves a URL into a FileEntry or DirectoryEntry you can work with. |
window.requestFileSystem(type, size, success, fail) | Returns the root of the filesystem. Pass LocalFileSystem.PERSISTENT or LocalFileSystem.TEMPORARY as the type. |
The plugin exposes a set of cordova.file.* path constants that point at the standard directories on each platform:
| Path constant | Description |
|---|---|
cordova.file.applicationDirectory | Read-only location of the installed app bundle. |
cordova.file.dataDirectory | Persistent, private storage for the app's own data. |
cordova.file.cacheDirectory | Cached files the operating system is free to clear. |
cordova.file.documentsDirectory | iOS user documents folder. |
cordova.file.externalDataDirectory | Android external storage for app data. |
cordova.file.tempDirectory | iOS temporary scratch directory. |
Once you have an entry, you work through these objects to navigate and move data:
| Object | Key members |
|---|---|
DirectoryEntry | getFile, getDirectory, createReader |
FileEntry | file, createWriter, remove |
FileReader | readAsText, readAsDataURL |
FileWriter | write, seek, truncate |
Create a text file in the persistent data directory and write a string into it:
window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(dir) {
dir.getFile("notes.txt", { create: true }, function(fileEntry) {
fileEntry.createWriter(function(writer) {
writer.write(new Blob(["Hello"], { type: "text/plain" }));
});
});
});
dataDirectory for files you must keep, and cacheDirectory for throwaway data the OS may clear.file:// or cdvfile:// URLs, not plain disk paths.