DedrisFrameworkDedrisFramework

← Plugins  /  File System

📂

File System

cordova-plugin-file
iOS Android 🌐 Browser

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.

Installation

Add the plugin to your Cordova project from the command line:

terminal
cordova plugin add cordova-plugin-file

Entry points

Two global functions get you a handle on the filesystem. Call them after the deviceready event has fired:

FunctionDescription
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.

Storage locations

The plugin exposes a set of cordova.file.* path constants that point at the standard directories on each platform:

Path constantDescription
cordova.file.applicationDirectoryRead-only location of the installed app bundle.
cordova.file.dataDirectoryPersistent, private storage for the app's own data.
cordova.file.cacheDirectoryCached files the operating system is free to clear.
cordova.file.documentsDirectoryiOS user documents folder.
cordova.file.externalDataDirectoryAndroid external storage for app data.
cordova.file.tempDirectoryiOS temporary scratch directory.

Core objects

Once you have an entry, you work through these objects to navigate and move data:

ObjectKey members
DirectoryEntrygetFile, getDirectory, createReader
FileEntryfile, createWriter, remove
FileReaderreadAsText, readAsDataURL
FileWriterwrite, seek, truncate

Usage example

Create a text file in the persistent data directory and write a string into it:

www/js/app.js
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" }));
    });
  });
});

Tips

📚 Source: the technical reference on this page (the entry points, storage locations and the install command) is based on the official Apache Cordova file documentation. Descriptions and examples have been rewritten in DedrisFramework's own words. Apache Cordova documentation is published by the Apache Software Foundation under the Apache License 2.0.
← Back to plugins Start a project