Skip to main content
@capgo/capacitor-file Files & Storage Open source

File Capacitor plugin

Full-featured file system plugin for reading, writing, and managing files and directories

Install

bun add @capgo/capacitor-file bunx cap sync
npm, pnpm or yarn
  • npm install @capgo/capacitor-file
  • pnpm add @capgo/capacitor-file
  • yarn add @capgo/capacitor-file

Guide

How to use File in Capacitor

Test on device

Download the Capgo app, then scan the QR code.

File plugin preview QR code

Using @capgo/capacitor-file

Capacitor File Plugin Implements file system operations similar to the Cordova File plugin.

Install

bun add @capgo/capacitor-file
bunx cap sync

bunx cap sync copies the plugin's native code into your native projects. Run it again after every plugin upgrade.

Import

import { CapacitorFile } from '@capgo/capacitor-file';

API at a glance

Method Description
requestFileSystem Request a file system.
resolveLocalFileSystemURL Resolve a file URL to an entry.
getFile Get a file entry.
getDirectory Get a directory entry.
readFile Read a file as text or base64.
readAsDataURL Read a file as a data URL (base64 with MIME type prefix).
writeFile Write data to a file.
appendFile Append data to a file.
deleteFile Delete a file.
mkdir Create a directory.
rmdir Delete a directory.
readdir Read directory contents.
stat Get metadata about a file or directory.
getMetadata Get metadata about a file or directory. Alias for stat().
rename Rename or move a file or directory.
move Move a file or directory. Alias for rename().
copy Copy a file or directory.
exists Check if a file or directory exists.
getUri Get the URI for a file.
truncate Truncate a file to a specified size.
getDirectories Get all known file system directories.
getFreeDiskSpace Get the free disk space in bytes.
checkPermissions Check the current permission status for file operations. On Android, this checks for external storage permissions. On iOS and web, this always returns 'granted' as no special permissions are needed.
requestPermissions Request permissions for file operations. On Android, this requests external storage permissions needed for accessing files outside the app's private directories. On iOS and web, this always returns 'granted' as no special permissions are needed.

Examples

requestFileSystem()

Request a file system.

import { CapacitorFile, FileSystemType } from '@capgo/capacitor-file';

const result = await CapacitorFile.requestFileSystem({ type: FileSystemType.TEMPORARY });
console.log(result);

resolveLocalFileSystemURL()

Resolve a file URL to an entry.

import { CapacitorFile } from '@capgo/capacitor-file';

const result = await CapacitorFile.resolveLocalFileSystemURL({ url: 'https://example.com' });
console.log(result);

getFile()

Get a file entry.

import { CapacitorFile } from '@capgo/capacitor-file';

const result = await CapacitorFile.getFile({ path: 'path/to/file' });
console.log(result);

getDirectory()

Get a directory entry.

import { CapacitorFile } from '@capgo/capacitor-file';

const result = await CapacitorFile.getDirectory({ path: 'path/to/file' });
console.log(result);

readFile()

Read a file as text or base64.

import { CapacitorFile } from '@capgo/capacitor-file';

const result = await CapacitorFile.readFile({ path: 'path/to/file' });
console.log(result);

readAsDataURL()

Read a file as a data URL (base64 with MIME type prefix).

import { CapacitorFile } from '@capgo/capacitor-file';

const result = await CapacitorFile.readAsDataURL({ path: 'path/to/file' });
console.log(result);

The table above lists the 24 core methods. Listener and version helpers, and the full contract of each method, are documented in the GitHub repository.

Listen to events

addListener returns a handle. Call handle.remove() when the screen unmounts, or CapacitorFile.removeAllListeners() to clear every listener.

Full reference

Keep going from Using @capgo/capacitor-file

If you are using Using @capgo/capacitor-file to plan storage and file handling, connect it with @capgo/capacitor-file for the implementation detail in @capgo/capacitor-file, Getting Started for the implementation detail in Getting Started, @capgo/capacitor-data-storage-sqlite for the implementation detail in @capgo/capacitor-data-storage-sqlite, Using @capgo/capacitor-data-storage-sqlite for the native capability in Using @capgo/capacitor-data-storage-sqlite, and @capgo/capacitor-uploader for the implementation detail in @capgo/capacitor-uploader.

FAQ

File plugin FAQ

How do I install the File plugin in a Capacitor app?

Run "bun add @capgo/capacitor-file" (or "npm install @capgo/capacitor-file"), then run "bunx cap sync" so the iOS and Android projects pick up the native code. Import it from "@capgo/capacitor-file" in your app code.

Does @capgo/capacitor-file work with React, Vue and Angular?

Yes. File is a Capacitor package, so it works with any web framework that runs inside Capacitor, including Ionic, React, Vue, Angular, Svelte and plain JavaScript.

Which Capacitor version does @capgo/capacitor-file support?

Capgo plugins follow Capacitor's major version: use the plugin major version that matches your Capacitor major version (for example plugin v8 with Capacitor 8). The compatibility table in the GitHub README lists the maintained versions.

Is @capgo/capacitor-file free and open source?

Yes. The source code is public on GitHub at https://github.com/Cap-go/capacitor-file/ and the package is free to install from npm. Bug reports and pull requests are welcome.

Can I update code that uses File without an App Store review?

Installing or upgrading the plugin changes native code, so it needs a new store build. After that, JavaScript, HTML and CSS changes that call the plugin can ship instantly with Capgo live updates.

Ship File changes without waiting for app review

Once the plugin is in your store build, Capgo live updates push your JavaScript, HTML and CSS changes to users in minutes.

Start with Capgo