FileSystem

The FileSystem API allows the user to read a specific directory, write the file to a specific location or read the file from a specific location.

//with npm
npm install @capacitor/filesystem


Usage

//write file
transition({
	type: MOBILE_WRITE_FILE,
	dir: "Documents",
    path: 'secrets/text.txt',
    data: "This is a test",
})

//read file
transition({
	type: MOBILE_READ_FILE,
	dir: "Documents",
    path: 'secrets/text.txt',
    readFileAsPath: false,
    readFileUri: true,
    getData: (result) => {
        console.log(`File content: ${result.data}`)
    }
})

//read directory
transition({
	type: MOBILE_READ_DIRECTORY,
	dir: "Documents",
    path: 'secrets/',,
    getData: (result) => {
        console.log(`Directory Files: ${result.files}`)
    }
})


API

ID Next States Description
MOBILE_WRITE_FILE MOBILE_ON_LOADED This transition writes the file in the specified path.
MOBILE_READ_FILE MOBILE_ON_LOADED This transition reads the file in the specified path.
MOBILE_READ_DIRECTORY MOBILE_ON_LOADED This transition reads the specified directory.


Properties

prop type default description
dir Directory "Documents" The Directory to store the file in or to read the file from. Directory can "Documents", "Data", "Cache", "External", "ExternalStorage"
path string The path of the file to read or write
data string The content to write in the file
readFileAsPath boolean Whether to read the file with full file path or path with directory. True reads the file with full file path and returns the contents of the file
readFileUri boolean Whether to read the file uri. False reads the file and returns content. True reads the file uri and returns the path
getData callback Provides result of the read file api


Capacitor configuration

## Android
//This API requires the following permissions be added to your AndroidManifest.xml to be able to read or write the files:
<!-- Filesystem API -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />