Skip to main content
Version: v6

@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

iOSโ€‹

To have files appear in the Files app, you must 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

ParamType
optionsReadFileOptions

Returns: Promise<ReadFileResult>

Since: 1.0.0


writeFile(...)โ€‹

writeFile(options: WriteFileOptions) => Promise<WriteFileResult>

Write a file to disk in the specified location on device

ParamType
optionsWriteFileOptions

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

ParamType
optionsAppendFileOptions

Since: 1.0.0


deleteFile(...)โ€‹

deleteFile(options: DeleteFileOptions) => Promise<void>

Delete a file from disk

ParamType
optionsDeleteFileOptions

Since: 1.0.0


mkdir(...)โ€‹

mkdir(options: MkdirOptions) => Promise<void>

Create a directory.

ParamType
optionsMkdirOptions

Since: 1.0.0


rmdir(...)โ€‹

rmdir(options: RmdirOptions) => Promise<void>

Remove a directory

ParamType
optionsRmdirOptions

Since: 1.0.0


readdir(...)โ€‹

readdir(options: ReaddirOptions) => Promise<ReaddirResult>

Return a list of files from the directory (not recursive)

ParamType
optionsReaddirOptions

Returns: Promise<ReaddirResult>

Since: 1.0.0


getUri(...)โ€‹

getUri(options: GetUriOptions) => Promise<GetUriResult>

Return full File URI for a path and directory

ParamType
optionsGetUriOptions

Returns: Promise<GetUriResult>

Since: 1.0.0


stat(...)โ€‹

stat(options: StatOptions) => Promise<StatResult>

Return data about a file

ParamType
optionsStatOptions

Returns: Promise<StatResult>

Since: 1.0.0


rename(...)โ€‹

rename(options: RenameOptions) => Promise<void>

Rename a file or directory

ParamType
optionsCopyOptions

Since: 1.0.0


copy(...)โ€‹

copy(options: CopyOptions) => Promise<CopyResult>

Copy a file or directory

ParamType
optionsCopyOptions

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.

ParamType
optionsDownloadFileOptions

Returns: Promise<DownloadFileResult>

Since: 5.1.0


addListener('progress', ...)โ€‹

addListener(eventName: 'progress', listenerFunc: ProgressListener) => Promise<PluginListenerHandle>

Add a listener to file download progress events.

ParamType
eventName'progress'
listenerFuncProgressListener

Returns: Promise<PluginListenerHandle>

Since: 5.1.0


removeAllListeners()โ€‹

removeAllListeners() => Promise<void>

Remove all listeners for this plugin.

Since: 5.2.0


Interfacesโ€‹

ReadFileResultโ€‹

PropTypeDescriptionSince
datastring | BlobThe 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โ€‹

PropTypeDescriptionSince
pathstringThe path of the file to read1.0.0
directoryDirectoryThe Directory to read the file from1.0.0
encodingEncodingThe 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 string1.0.0

WriteFileResultโ€‹

PropTypeDescriptionSince
uristringThe uri where the file was written into1.0.0

WriteFileOptionsโ€‹

PropTypeDescriptionDefaultSince
pathstringThe path of the file to write1.0.0
datastring | BlobThe data to write Note: Blob data is only supported on Web.1.0.0
directoryDirectoryThe Directory to store the file in1.0.0
encodingEncodingThe encoding to write the file in. If not provided, data is written as base64 encoded. Pass Encoding.UTF8 to write data as string1.0.0
recursivebooleanWhether to create any missing parent directories.false1.0.0

AppendFileOptionsโ€‹

PropTypeDescriptionSince
pathstringThe path of the file to append1.0.0
datastringThe data to write1.0.0
directoryDirectoryThe Directory to store the file in1.0.0
encodingEncodingThe encoding to write the file in. If not provided, data is written as base64 encoded. Pass Encoding.UTF8 to write data as string1.0.0

DeleteFileOptionsโ€‹

PropTypeDescriptionSince
pathstringThe path of the file to delete1.0.0
directoryDirectoryThe Directory to delete the file from1.0.0

MkdirOptionsโ€‹

PropTypeDescriptionDefaultSince
pathstringThe path of the new directory1.0.0
directoryDirectoryThe Directory to make the new directory in1.0.0
recursivebooleanWhether to create any missing parent directories as well.false1.0.0

RmdirOptionsโ€‹

PropTypeDescriptionDefaultSince
pathstringThe path of the directory to remove1.0.0
directoryDirectoryThe Directory to remove the directory from1.0.0
recursivebooleanWhether to recursively remove the contents of the directoryfalse1.0.0

ReaddirResultโ€‹

PropTypeDescriptionSince
filesFileInfo[]List of files and directories inside the directory1.0.0

FileInfoโ€‹

PropTypeDescriptionSince
namestringName of the file or directory.
type'file' | 'directory'Type of the file.4.0.0
sizenumberSize of the file in bytes.4.0.0
ctimenumberTime of creation in milliseconds. It's not available on Android 7 and older devices.4.0.0
mtimenumberTime of last modification in milliseconds.4.0.0
uristringThe uri of the file.4.0.0

ReaddirOptionsโ€‹

PropTypeDescriptionSince
pathstringThe path of the directory to read1.0.0
directoryDirectoryThe Directory to list files from1.0.0

GetUriResultโ€‹

PropTypeDescriptionSince
uristringThe uri of the file1.0.0

GetUriOptionsโ€‹

PropTypeDescriptionSince
pathstringThe path of the file to get the URI for1.0.0
directoryDirectoryThe Directory to get the file under1.0.0

StatResultโ€‹

PropTypeDescriptionSince
type'file' | 'directory'Type of the file.1.0.0
sizenumberSize of the file in bytes.1.0.0
ctimenumberTime of creation in milliseconds. It's not available on Android 7 and older devices.1.0.0
mtimenumberTime of last modification in milliseconds.1.0.0
uristringThe uri of the file1.0.0

StatOptionsโ€‹

PropTypeDescriptionSince
pathstringThe path of the file to get data about1.0.0
directoryDirectoryThe Directory to get the file under1.0.0

CopyOptionsโ€‹

PropTypeDescriptionSince
fromstringThe existing file or directory1.0.0
tostringThe destination file or directory1.0.0
directoryDirectoryThe Directory containing the existing file or directory1.0.0
toDirectoryDirectoryThe Directory containing the destination file or directory. If not supplied will use the 'directory' parameter as the destination1.0.0

CopyResultโ€‹

PropTypeDescriptionSince
uristringThe uri where the file was copied into4.0.0

PermissionStatusโ€‹

PropType
publicStoragePermissionState

DownloadFileResultโ€‹

PropTypeDescriptionSince
pathstringThe path the file was downloaded to.5.1.0
blobBlobThe blob data of the downloaded file. This is only available on web.5.1.0

DownloadFileOptionsโ€‹

PropTypeDescriptionDefaultSince
pathstringThe path the downloaded file should be moved to.5.1.0
directoryDirectoryThe directory to write the file to. If this option is used, filePath can be a relative path rather than absolute. The default is the DATA directory.5.1.0
progressbooleanAn optional listener function to receive downloaded progress events. If this option is used, progress event should be dispatched on every chunk received. Chunks are throttled to every 100ms on Android/iOS to avoid slowdowns.5.1.0
recursivebooleanWhether to create any missing parent directories.false5.1.2

PluginListenerHandleโ€‹

PropType
remove() => Promise<void>

ProgressStatusโ€‹

PropTypeDescriptionSince
urlstringThe url of the file being downloaded.5.1.0
bytesnumberThe number of bytes downloaded so far.5.1.0
contentLengthnumberThe total number of bytes to download for this file.5.1.0

Type Aliasesโ€‹

RenameOptionsโ€‹

CopyOptions

PermissionStateโ€‹

'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'

ProgressListenerโ€‹

A listener function that receives progress events.

(progress: ProgressStatus): void

Enumsโ€‹

Directoryโ€‹

MembersValueDescriptionSince
Documents'DOCUMENTS'The Documents directory. On iOS it's the app's documents directory. Use this directory to store user-generated content. On Android it's the Public Documents folder, so it's accessible from other apps. It's not accesible on Android 10 unless the app enables legacy External Storage by adding android:requestLegacyExternalStorage="true" in the application tag in the AndroidManifest.xml. On Android 11 or newer the app can only access the files/folders the app created.1.0.0
Data'DATA'The Data directory. On iOS it will use the Documents directory. On Android it's the directory holding application files. Files will be deleted when the application is uninstalled.1.0.0
Library'LIBRARY'The Library directory. On iOS it will use the Library directory. On Android it's the directory holding application files. Files will be deleted when the application is uninstalled.1.1.0
Cache'CACHE'The Cache directory. Can be deleted in cases of low memory, so use this directory to write app-specific files. that your app can re-create easily.1.0.0
External'EXTERNAL'The external directory. On iOS it will use the Documents directory. On Android it's the directory on the primary shared/external storage device where the application can place persistent files it owns. These files are internal to the applications, and not typically visible to the user as media. Files will be deleted when the application is uninstalled.1.0.0
ExternalStorage'EXTERNAL_STORAGE'The external storage directory. On iOS it will use the Documents directory. On Android it's the primary shared/external storage directory. It's not accesible on Android 10 unless the app enables legacy External Storage by adding android:requestLegacyExternalStorage="true" in the application tag in the AndroidManifest.xml. It's not accesible on Android 11 or newer.1.0.0

Encodingโ€‹

MembersValueDescriptionSince
UTF8'utf8'Eight-bit UCS Transformation Format1.0.0
ASCII'ascii'Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set This encoding is only supported on Android.1.0.0
UTF16'utf16'Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark This encoding is only supported on Android.1.0.0