@capacitor/filesystem
The Filesystem API provides a NodeJS-like API for working with files on the device.
Install
npm install @capacitor/filesystem
npx cap sync
Android
If using Directory.Documents
or
Directory.ExternalStorage
, this API requires the following permissions be added to your AndroidManifest.xml
:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Read about Setting Permissions in the Android Guide for more information on setting Android permissions.
Note that Directory.Documents
and
Directory.ExternalStorage
are only available on Android 9 or older.
Understanding Directories and Files
iOS and Android have additional layers of separation between files, such as special directories that are backed up to the Cloud, or ones for storing Documents. The Filesystem API offers a simple way to scope each operation to a specific special directory on the device.
Additionally, the Filesystem API supports using full file://
paths, or reading content://
files on Android. Simply leave out the directory
param to use a full file path.
Example
import { Filesystem, Directory, Encoding } from '@capacitor/filesystem';
const writeSecretFile = async () => {
await Filesystem.writeFile({
path: 'secrets/text.txt',
data: "This is a test",
directory: Directory.Documents,
encoding: Encoding.UTF8,
});
};
const readSecretFile = async () => {
const contents = await Filesystem.readFile({
path: 'secrets/text.txt',
directory: Directory.Documents,
encoding: Encoding.UTF8,
});
console.log('secrets:', contents);
};
const deleteSecretFile = async () => {
await Filesystem.deleteFile({
path: 'secrets/text.txt',
directory: Directory.Documents,
});
};
const readFilePath = async () => {
// Here's an example of reading a file with a full file path. Use this to
// read binary data (base64 encoded) from plugins that return File URIs, such as
// the Camera.
const contents = await Filesystem.readFile({
path: 'file:///var/mobile/Containers/Data/Application/22A433FD-D82D-4989-8BE6-9FC49DEA20BB/Documents/text.txt'
});
console.log('data:', contents);
};
API
readFile(...)
readFile(options: ReadFileOptions) => Promise<ReadFileResult>
Read a file from disk
Param | Type |
---|---|
options |
|
Returns:
Promise<ReadFileResult>
Since: 1.0.0
writeFile(...)
writeFile(options: WriteFileOptions) => Promise<WriteFileResult>
Write a file to disk in the specified location on device
Param | Type |
---|---|
options |
|
Returns:
Promise<WriteFileResult>
Since: 1.0.0
appendFile(...)
appendFile(options: AppendFileOptions) => Promise<void>
Append to a file on disk in the specified location on device
Param | Type |
---|---|
options |
|
Since: 1.0.0
deleteFile(...)
deleteFile(options: DeleteFileOptions) => Promise<void>
Delete a file from disk
Param | Type |
---|---|
options |
|
Since: 1.0.0
mkdir(...)
mkdir(options: MkdirOptions) => Promise<void>
Create a directory.
Param | Type |
---|---|
options |
|
Since: 1.0.0
rmdir(...)
rmdir(options: RmdirOptions) => Promise<void>
Remove a directory
Param | Type |
---|---|
options |
|
Since: 1.0.0
readdir(...)
readdir(options: ReaddirOptions) => Promise<ReaddirResult>
Return a list of files from the directory (not recursive)
Param | Type |
---|---|
options |
|
Returns:
Promise<ReaddirResult>
Since: 1.0.0
getUri(...)
getUri(options: GetUriOptions) => Promise<GetUriResult>
Return full File URI for a path and directory
Param | Type |
---|---|
options |
|
Returns:
Promise<GetUriResult>
Since: 1.0.0
stat(...)
stat(options: StatOptions) => Promise<StatResult>
Return data about a file
Param | Type |
---|---|
options |
|
Returns:
Promise<StatResult>
Since: 1.0.0
rename(...)
rename(options: RenameOptions) => Promise<void>
Rename a file or directory
Param | Type |
---|---|
options |
|
Since: 1.0.0