@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
Apple Privacy Manifest Requirementsโ
Apple mandates that app developers now specify approved reasons for API usage to enhance user privacy. By May 1st, 2024, it's required to include these reasons when submitting apps to the App Store Connect.
When using this specific plugin in your app, you must create a PrivacyInfo.xcprivacy
file in /ios/App
or use the VS Code Extension to generate it, specifying the usage reasons.
For detailed steps on how to do this, please see the Capacitor Docs.
For this plugin, the required dictionary key is NSPrivacyAccessedAPICategoryFileTimestamp and the recommended reason is C617.1.
Example PrivacyInfo.xcprivacyโ
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSPrivacyAccessedAPITypes</key>
<array>
<!-- Add this dict entry to the array if the PrivacyInfo file already exists -->
<dict>
<key>NSPrivacyAccessedAPIType</key>
<string>NSPrivacyAccessedAPICategoryFileTimestamp</string>
<key>NSPrivacyAccessedAPITypeReasons</key>
<array>
<string>C617.1</string>
</array>
</dict>
</array>
</dict>
</plist>
iOSโ
To have files appear in the Files app, you must also set the following keys to YES
in Info.plist
:
UIFileSharingEnabled
(Application supports iTunes file sharing
)LSSupportsOpeningDocumentsInPlace
(Supports opening documents in place
)
Read about Configuring iOS for help.
Androidโ
If using Directory.Documents
or Directory.ExternalStorage
, in Android 10 and older, 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.ExternalStorage
is only available on Android 9 or older and Directory.Documents
only allows to access the files/folders created by your app on Android on Android 11 and newer.
Working with large files may require you to add android:largeHeap="true"
to the <application>
tag in AndroidManifest.xml
.
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
copy(...)โ
copy(options: CopyOptions) => Promise<CopyResult>
Copy a file or directory
Param | Type |
---|---|
options |
|
Returns:
Promise<CopyResult>
Since: 1.0.0
checkPermissions()โ
checkPermissions() => Promise<PermissionStatus>
Check read/write permissions.
Required on Android, only when using Directory.Documents
or
Directory.ExternalStorage
.
Returns:
Promise<PermissionStatus>
Since: 1.0.0
requestPermissions()โ
requestPermissions() => Promise<PermissionStatus>
Request read/write permissions.
Required on Android, only when using Directory.Documents
or
Directory.ExternalStorage
.
Returns:
Promise<PermissionStatus>
Since: 1.0.0
downloadFile(...)โ
downloadFile(options: DownloadFileOptions) => Promise<DownloadFileResult>
Perform a http request to a server and download the file to the specified destination.
Param | Type |
---|---|
options |
|
Returns:
Promise<DownloadFileResult>
Since: 5.1.0
addListener('progress', ...)โ
addListener(eventName: 'progress', listenerFunc: ProgressListener) => Promise<PluginListenerHandle>
Add a listener to file download progress events.
Param | Type |
---|---|
eventName | 'progress' |
listenerFunc |
|
Returns:
Promise<PluginListenerHandle>
Since: 5.1.0
removeAllListeners()โ
removeAllListeners() => Promise<void>
Remove all listeners for this plugin.
Since: 5.2.0
Interfacesโ
ReadFileResultโ
Prop | Type | Description | Since |
---|---|---|---|
data | string | Blob | The representation of the data contained in the file Note: Blob is only available on Web. On native, the data is returned as a string. | 1.0.0 |
ReadFileOptionsโ
Prop | Type | Description | Since |
---|---|---|---|
path | string | The path of the file to read | 1.0.0 |
directory |
| The Directory to read the file from | 1.0.0 |
encoding |
| The encoding to read the file in, if not provided, data is read as binary and returned as base64 encoded. Pass Encoding.UTF8 to read data as string | 1.0.0 |
WriteFileResultโ
Prop | Type | Description | Since |
---|---|---|---|
uri | string | The uri where the file was written into | 1.0.0 |
WriteFileOptionsโ
Prop | Type | Description | Default | Since |
---|---|---|---|---|
path | string | The path of the file to write | 1.0.0 | |
data | string | Blob | The data to write Note: Blob data is only supported on Web. | 1.0.0 | |
directory |
| The Directory to store the file in | 1.0.0 | |
encoding |
| The encoding to write the file in. If not provided, data is written as base64 encoded. Pass Encoding.UTF8 to write data as string | 1.0.0 | |
recursive | boolean | Whether to create any missing parent directories. | false | 1.0.0 |
AppendFileOptionsโ
Prop | Type | Description | Since |
---|---|---|---|
path | string | The path of the file to append | 1.0.0 |
data | string | The data to write | 1.0.0 |
directory |
| The Directory to store the file in | 1.0.0 |
encoding |
| The encoding to write the file in. If not provided, data is written as base64 encoded. Pass Encoding.UTF8 to write data as string | 1.0.0 |
DeleteFileOptionsโ
Prop | Type | Description | Since |
---|---|---|---|
path | string | The path of the file to delete | 1.0.0 |
directory |
| The Directory to delete the file from | 1.0.0 |
MkdirOptionsโ
Prop | Type | Description | Default | Since |
---|---|---|---|---|
path | string | The path of the new directory | 1.0.0 | |
directory |
| The Directory to make the new directory in | 1.0.0 | |
recursive | boolean | Whether to create any missing parent directories as well. | false | 1.0.0 |
RmdirOptionsโ
Prop | Type | Description | Default | Since |
---|---|---|---|---|
path | string | The path of the directory to remove | 1.0.0 | |
directory |
| The Directory to remove the directory from | 1.0.0 | |
recursive | boolean | Whether to recursively remove the contents of the directory | false | 1.0.0 |
ReaddirResultโ
Prop | Type | Description | Since |
---|---|---|---|
files | FileInfo[] | List of files and directories inside the directory | 1.0.0 |
FileInfoโ
Prop | Type | Description | Since |
---|---|---|---|
name | string | Name of the file or directory. | |
type | 'file' | 'directory' | Type of the file. | 4.0.0 |
size | number | Size of the file in bytes. | 4.0.0 |
ctime | number | Time of creation in milliseconds. It's not available on Android 7 and older devices. | 4.0.0 |
mtime | number | Time of last modification in milliseconds. | 4.0.0 |
uri | string | The uri of the file. | 4.0.0 |
ReaddirOptionsโ
Prop | Type | Description | Since |
---|---|---|---|
path | string | The path of the directory to read | 1.0.0 |
directory |
| The Directory to list files from | 1.0.0 |
GetUriResultโ
Prop | Type | Description | Since |
---|---|---|---|
uri | string | The uri of the file | 1.0.0 |
GetUriOptionsโ
Prop | Type | Description | Since |
---|---|---|---|
path | string | The path of the file to get the URI for | 1.0.0 |
directory |
| The Directory to get the file under | 1.0.0 |
StatResultโ
Prop | Type | Description | Since |
---|---|---|---|
type | 'file' | 'directory' | Type of the file. | 1.0.0 |
size | number | Size of the file in bytes. | 1.0.0 |
ctime | number | Time of creation in milliseconds. It's not available on Android 7 and older devices. | 1.0.0 |
mtime | number | Time of last modification in milliseconds. | 1.0.0 |
uri | string | The uri of the file | 1.0.0 |