React Native · APIs and utilities
File system
File system: read/write, folders, download/upload, picking files and media, save as, saving to the gallery, opening a file.
14 exports
Import
FileSystem
moduleFile system: read/write, folders, download/upload, picking files and media, save as, saving to the gallery, opening a file.
| Name | Type | Description |
|---|---|---|
documentDirectory | string | The app's persistent documents folder (backed up). |
cacheDirectory | string | Cache folder (the system may delete it to free space). |
tempDirectory | string | Temporary folder. |
exists | (path: string) => Promise<boolean> | Whether it exists. |
info | (path: string) => Promise<FileInfo | null> | Info; null if missing. |
readFile | (path: string, encoding?: FileEncoding) => Promise<string> | Reads a file as text (or base64). |
writeFile | (path: string, data: string, options?: { encoding?: FileEncoding; append?: boolean; }) => Promise<void> | Writes to a file (folders are created). append adds to the end. |
readJson | <T = unknown>(path: string) => Promise<T> | Reads JSON. |
writeJson | (path: string, value: unknown) => Promise<void> | Writes JSON. |
delete | (path: string, options?: { recursive?: boolean; }) => Promise<void> | Deletes (does nothing if missing). Folders are deleted with their contents by default. |
copy | (from: string, to: string) => Promise<FileInfo> | Copies (overwrites the target if it exists). On Android the source may be content://. |
move | (from: string, to: string) => Promise<FileInfo> | Moves / renames. |
mkdir | (path: string) => Promise<void> | Creates a folder (with parent folders). |
list | (path: string) => Promise<FileInfo[]> | Contents of a folder (sorted by name). |
freeSpace | () => Promise<number> | Free space (bytes). |
download | (url: string, options?: DownloadOptions) => Promise<DownloadResult> | Downloads (with progress, cancellable, follows redirects). The file is first written temporarily and moved to the target when finished. |
upload | (url: string, options: UploadOptions) => Promise<UploadResult> | Uploads files (multipart/form-data, with progress, cancellable). The file is streamed without loading it into memory; suitable for large videos. |
pickDocument | (options?: PickDocumentOptions) => Promise<PickedFile[]> | The system file picker (no permission needed). An empty array if dismissed. |
pickMedia | (options?: PickMediaOptions) => Promise<PickedFile[]> | The system photo/video picker (no permission needed; Android 11+ Photo Picker, iOS PHPicker). An empty array if dismissed. |
saveAs | (path: string, options?: { name?: string; mimeType?: string; }) => Promise<string | null> | "Save as": the user picks the location and name (Android create document, iOS Files). The saved address, or null if dismissed. |
saveToGallery | (path: string, options?: SaveToGalleryOptions) => Promise<string> | Saves an image/video to the gallery. On iOS it needs ui.config permissions.photosAdd (the code is only compiled then); Android 10+ asks for no permission. |
open | (path: string, options?: { mimeType?: string; dialogTitle?: string; }) => Promise<void> | Opens a file: iOS preview (Quick Look), on Android with a suitable app. |
joinPath
functionJoins paths (joinPath(FileSystem.documentDirectory, "notes", "a.txt")).
FileInfo
interfaceFile or folder info.
| Name | Type | Description |
|---|---|---|
path | string | Absolute path (the address itself for Android content:// addresses). |
uri | string | file:// address (or content://). |
name | string | File or folder name. |
size | number | null | Bytes; null for a folder or when unknown. |
isDirectory | boolean | Whether it is a folder. |
modified | number | null | Last modified (ms, epoch). |
type | string | null | MIME type (from the extension). |
PickedFile
interfaceA picked file.
| Name | Type | Description |
|---|---|---|
uri | string | Address of the file (file://). By default it is copied to the cache: it is persistent, readable and uploadable. On Android copy: false returns a content:// address. |
name | string | File name. |
size | number | null | Bytes; null if unknown. |
type | string | null | MIME type; null if unknown. |
width? | number | Image/video width (pixels). |
height? | number | Image/video height (pixels). |
duration? | number | Video duration (s). |
FileEncoding
typeText encoding.
PickDocumentOptions
interfaceOptions of FileSystem.pickDocument.
| Name | Type | Default | Description |
|---|---|---|---|
multiple? | boolean | false | Multiple files. |
limit? | number | Maximum selection (with multiple). | |
types? | readonly string[] | any type | Allowed types: MIME ("application/pdf", "image/*") or an extension (".docx", iOS). |
copy? | boolean | true | Android: copy to the cache (false: a content:// address). |
PickMediaOptions
interfaceOptions of FileSystem.pickMedia.
| Name | Type | Default | Description |
|---|---|---|---|
mediaType? | "image" | "video" | "all" | "image" | |
multiple? | boolean | false | Multiple selection. |
limit? | number | Maximum selection (with multiple). | |
compatible? | boolean | true | iOS: a compatible format (JPEG/MP4) instead of HEIC/MOV. |
copy? | boolean | true | Android: copy to the cache. |
TransferProgress
interfaceProgress.
| Name | Type | Description |
|---|---|---|
loaded | number | Bytes transferred. |
total | number | null | Total bytes (null if unknown). |
fraction | number | null | 0–1 (null if the total is unknown). |
DownloadOptions
interfaceOptions of FileSystem.download.
| Name | Type | Default | Description |
|---|---|---|---|
to? | string | Target path. If omitted, under downloads/ in the cache with the name the server gives. | |
headers? | Readonly<Record<string, string>> | Request headers. | |
onProgress? | (progress: TransferProgress) => void | Progress (at most ~10 times/s). | |
signal? | AbortSignal | Cancel: AbortController().signal. A cancelled operation rejects with E_CANCELLED. | |
timeout? | number | 60000 | Timeout (ms). |
DownloadResult
interfaceDownload result.
| Name | Type | Description |
|---|---|---|
path | string | Absolute path of the saved file. |
uri | string | file:// address of the saved file. |
status | number | HTTP status code. |
headers | Record<string, string> | Response headers. |
size | number | Bytes. |
type | string | null | MIME type from the response; null if unknown. |
UploadFile
interfaceFile to upload.
| Name | Type | Default | Description |
|---|---|---|---|
path | string | Path or address (file://, also content:// on Android). | |
field? | string | "file" | Name of the form field. |
name? | string | the file's name | Name sent to the server. |
type? | string | from the extension | MIME type. |
UploadOptions
interfaceOptions of FileSystem.upload.
| Name | Type | Default | Description |
|---|---|---|---|
method? | "POST" | "PUT" | "PATCH" | "POST" | |
files | readonly (UploadFile | string)[] | Files (multipart/form-data). | |
fields? | Readonly<Record<string, string | number | boolean>> | Extra form fields. | |
raw? | boolean | false | The body is only the first file itself (e.g. a PUT to an S3 signed URL). |
headers? | Readonly<Record<string, string>> | Request headers. | |
onProgress? | (progress: TransferProgress) => void | Progress (at most ~10 times/s). | |
signal? | AbortSignal | Cancel: AbortController().signal. A cancelled operation rejects with E_CANCELLED. | |
timeout? | number | 60000 | Timeout (ms). |
UploadResult
interfaceUpload result.
| Name | Type | Description |
|---|---|---|
status | number | HTTP status code. |
body | string | Response body (text). |
headers | Record<string, string> | Response headers. |
SaveToGalleryOptions
interfaceOptions of FileSystem.saveToGallery.
| Name | Type | Description |
|---|---|---|
album? | string | Android: album (Pictures/<album>). |