Filesystem
The Filesystem API provides a NodeJS-like API for working with files on the device.
readFile(...)
writeFile(...)
appendFile(...)
deleteFile(...)
mkdir(...)
rmdir(...)
readdir(...)
getUri(...)
stat(...)
rename(...)
copy(...)
- Interfaces
- Enums
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 { Plugins, FilesystemDirectory, FilesystemEncoding } from '@capacitor/core';
const { Filesystem } = Plugins;
async fileWrite() {
try {
const result = await Filesystem.writeFile({
path: 'secrets/text.txt',
data: "This is a test",
directory: FilesystemDirectory.Documents,
encoding: FilesystemEncoding.UTF8
})
console.log('Wrote file', result);
} catch(e) {
console.error('Unable to write file', e);
}
}
async fileRead() {
let contents = await Filesystem.readFile({
path: 'secrets/text.txt',
directory: FilesystemDirectory.Documents,
encoding: FilesystemEncoding.UTF8
});
console.log(contents);
}
async fileAppend() {
await Filesystem.appendFile({
path: 'secrets/text.txt',
data: "MORE TESTS",
directory: FilesystemDirectory.Documents,
encoding: FilesystemEncoding.UTF8
});
}
async fileDelete() {
await Filesystem.deleteFile({
path: 'secrets/text.txt',
directory: FilesystemDirectory.Documents
});
}
async mkdir() {
try {
let ret = await Filesystem.mkdir({
path: 'secrets',
directory: FilesystemDirectory.Documents,
recursive: false // like mkdir -p
});
} catch(e) {
console.error('Unable to make directory', e);
}
}
async rmdir() {
try {
let ret = await Filesystem.rmdir({
path: 'secrets',
directory: FilesystemDirectory.Documents,
recursive: false,
});
} catch(e) {
console.error('Unable to remove directory', e);
}
}
async readdir() {
try {
let ret = await Filesystem.readdir({
path: 'secrets',
directory: FilesystemDirectory.Documents
});
} catch(e) {
console.error('Unable to read dir', e);
}
}
async stat() {
try {
let ret = await Filesystem.stat({
path: 'secrets/text.txt',
directory: FilesystemDirectory.Documents
});
} catch(e) {
console.error('Unable to stat file', e);
}
}
async readFilePath() {
// 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.
try {
let data = await Filesystem.readFile({
path: 'file:///var/mobile/Containers/Data/Application/22A433FD-D82D-4989-8BE6-9FC49DEA20BB/Documents/text.txt'
})
}
}
async rename() {
try {
// This example moves the file within the same 'directory'
let ret = await Filesystem.rename({
from: 'text.txt',
to: 'text2.txt',
directory: FilesystemDirectory.Documents
});
} catch(e) {
console.error('Unable to rename file', e);
}
}
async copy() {
try {
// This example copies a file within the documents directory
let ret = await Filesystem.copy({
from: 'text.txt',
to: 'text2.txt',
directory: FilesystemDirectory.Documents
});
} catch(e) {
console.error('Unable to copy file', e);
}
}
API
readFile(...)
readFile(options: FileReadOptions) => Promise<FileReadResult>
Read a file from disk
Param | Type | Description |
---|---|---|
options |
| options for the file read |
Returns:
Promise<FileReadResult>
writeFile(...)
writeFile(options: FileWriteOptions) => Promise<FileWriteResult>
Write a file to disk in the specified location on device
Param | Type | Description |
---|---|---|
options |
| options for the file write |
Returns:
Promise<FileWriteResult>
appendFile(...)
appendFile(options: FileAppendOptions) => Promise<FileAppendResult>
Append to a file on disk in the specified location on device
Param | Type | Description |
---|---|---|
options |
| options for the file append |
Returns:
Promise<FileAppendResult>
deleteFile(...)
deleteFile(options: FileDeleteOptions) => Promise<FileDeleteResult>
Delete a file from disk
Param | Type | Description |
---|---|---|
options |
| options for the file delete |
Returns:
Promise<FileDeleteResult>
mkdir(...)
mkdir(options: MkdirOptions) => Promise<MkdirResult>
Create a directory.
Param | Type | Description |
---|---|---|
options |
| options for the mkdir |
Returns:
Promise<MkdirResult>
rmdir(...)
rmdir(options: RmdirOptions) => Promise<RmdirResult>
Remove a directory
Param | Type | Description |
---|---|---|
options |
| the options for the directory remove |
Returns:
Promise<RmdirResult>
readdir(...)
readdir(options: ReaddirOptions) => Promise<ReaddirResult>
Return a list of files from the directory (not recursive)
Param | Type | Description |
---|---|---|
options |
| the options for the readdir operation |
Returns:
Promise<ReaddirResult>
getUri(...)
getUri(options: GetUriOptions) => Promise<GetUriResult>