Panduan Memulai
Salin prompt setup yang berisi langkah install dan panduan markdown lengkap untuk plugin ini.
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-file`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/file/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
Anda bisa menggunakan AI-Assisted Setup kami untuk menginstal plugin ini. Tambahkan skill Capgo ke tool AI Anda dengan command berikut:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-pluginsLalu gunakan prompt berikut:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-file` plugin in my project.Jika Anda lebih memilih Manual Setup, instal plugin dengan menjalankan perintah berikut dan ikuti petunjuk khusus platform di bawah ini:
npm install @capgo/capacitor-filenpx cap syncimport { CapacitorFile } from '@capgo/capacitor-file';Mulai cepat
Bagian berjudul “Mulai cepat”import { CapacitorFile, FileSystemType } from '@capgo/capacitor-file';
const result = await CapacitorFile.requestFileSystem({ type: FileSystemType.TEMPORARY });console.log(result);Contoh: resolveLocalFileSystemURL
Bagian berjudul “Example: resolveLocalFileSystemURL”import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.resolveLocalFileSystemURL({ url: 'file:///data/user/0/com.example.app/files/report.pdf',});console.log(result);Contoh: getFile
Bagian berjudul “Example: getFile”import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getFile({ path: 'path/to/file' });console.log(result);Kesalahan umum
Bagian berjudul “Kesalahan umum”- Izin: panggil
checkPermissions()kemudianrequestPermissions()saat dibutuhkan. - Listener: hapus handle saat screen unmount.
- Sumber kebenaran tunggal: percayai
src/definitions.tsdi repo GitHub.
Ringkasan API
Bagian berjudul “Ikhtisar API”requestFileSystem
Bagian berjudul “requestFileSystem”Meminta file system.
import { CapacitorFile, FileSystemType } from '@capgo/capacitor-file';
const result = await CapacitorFile.requestFileSystem({ type: FileSystemType.TEMPORARY });console.log(result);resolveLocalFileSystemURL
Bagian berjudul “resolveLocalFileSystemURL”Me-resolve URL file menjadi entry.
import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.resolveLocalFileSystemURL({ url: 'https://example.com' });console.log(result);getFile
Bagian berjudul “getFile”Mendapatkan entry file.
import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getFile({ path: 'path/to/file' });console.log(result);getDirectory
Bagian berjudul “getDirectory”Dapatkan entri direktori.
import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getDirectory({ path: 'path/to/file' });console.log(result);readFile
Bagian berjudul “readFile”Baca file sebagai teks atau base64.
import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.readFile({ path: 'path/to/file' });console.log(result);readAsDataURL
Bagian berjudul “readAsDataURL”Baca file sebagai data URL (base64 dengan prefix MIME type).
import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.readAsDataURL({ path: 'path/to/file' });console.log(result);writeFile
Bagian berjudul “writeFile”Menulis data ke dalam file.
import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.writeFile({ path: 'path/to/file', data: 'data',});console.log(result);appendFile
Bagian berjudul “appendFile”Tambahkan data ke file.
import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.appendFile({ path: 'path/to/file', data: 'data',});console.log(result);deleteFile
Bagian berjudul “deleteFile”Hapus file.
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.deleteFile({ path: 'path/to/file' });Buat direktori.
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.mkdir({ path: 'path/to/file' });Hapus direktori.
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.rmdir({ path: 'path/to/file' });readdir
Bagian berjudul “readdir”Baca isi direktori.
import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.readdir({ path: 'path/to/file' });console.log(result);Dapatkan metadata file atau direktori.
import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.stat({ path: 'path/to/file' });console.log(result);getMetadata
Bagian berjudul “getMetadata”Dapatkan metadata tentang file atau direktori. Alias untuk stat().
import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getMetadata({ path: 'path/to/file' });console.log(result);rename
Bagian berjudul “rename”Ubah nama atau pindahkan file atau direktori.
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.rename({ from: 'from', to: 'to',});Memindahkan file atau direktori. Alias untuk rename().
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.move({ from: 'from', to: 'to',});Salin file atau direktori.
import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.copy({ from: 'from', to: 'to',});console.log(result);exists
Bagian berjudul “exists”Cek apakah file atau direktori ada.
import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.exists({ path: 'path/to/file' });console.log(result);getUri
Bagian berjudul “getUri”Dapatkan URI file.
import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getUri({ path: 'path/to/file' });console.log(result);truncate
Bagian berjudul “truncate”Potong file ke ukuran tertentu.
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.truncate({ path: 'path/to/file' });getDirectories
Bagian berjudul “getDirectories”Dapatkan semua direktori file system yang diketahui.
import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getDirectories();console.log(result);getFreeDiskSpace
Bagian berjudul “getFreeDiskSpace”Dapatkan sisa ruang disk dalam byte.
import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getFreeDiskSpace();console.log(result);checkPermissions
Bagian berjudul “checkPermissions”Cek status izin saat ini untuk operasi file. Di Android, ini mengecek izin penyimpanan eksternal. Di iOS dan web, ini selalu mengembalikan ‘granted’ karena tidak butuh izin khusus.
import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.checkPermissions();console.log(result);requestPermissions
Bagian berjudul “requestPermissions”Minta izin untuk operasi file. Di Android, ini meminta izin penyimpanan eksternal yang diperlukan untuk mengakses file di luar direktori privat aplikasi. Di iOS dan web, ini selalu mengembalikan ‘granted’ karena tidak butuh izin khusus.
import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.requestPermissions();console.log(result);Referensi Tipe
Bagian berjudul “Referensi Tipe”RequestFileSystemOptions
Bagian berjudul “RequestFileSystemOptions”Opsi untuk meminta file system.
export interface RequestFileSystemOptions { /** The type of file system to request */ type: FileSystemType; /** Requested size in bytes (may not be enforced on all platforms) */ size?: number;}FileSystem
Bagian berjudul “FileSystem”Merepresentasikan file system.
export interface FileSystem { /** The name of the file system */ name: string; /** The root directory of the file system */ root: DirectoryEntry;}ResolveURLOptions
Bagian berjudul “ResolveURLOptions”Opsi untuk resolve URL ke sebuah entry.
export interface ResolveURLOptions { /** The URL to resolve (file:// or cdvfile://) */ url: string;}Merepresentasikan entry file atau direktori.
export interface Entry { /** True if this is a file */ isFile: boolean; /** True if this is a directory */ isDirectory: boolean; /** The name of the file or directory */ name: string; /** The full path relative to the filesystem root */ fullPath: string; /** The native file:// URI */ nativeURL: string;}GetFileOptions
Bagian berjudul “GetFileOptions”Opsi untuk mengambil file.
export interface GetFileOptions { /** Path to the file */ path: string; /** Base directory */ directory?: Directory; /** Options for creating the file */ options?: GetOptions;}FileEntry
Bagian berjudul “FileEntry”Merepresentasikan entry file.
export interface FileEntry extends Entry { isFile: true; isDirectory: false;}GetDirectoryOptions
Bagian berjudul “GetDirectoryOptions”Opsi untuk mengambil direktori.
export interface GetDirectoryOptions { /** Path to the directory */ path: string; /** Base directory */ directory?: Directory; /** Options for creating the directory */ options?: GetOptions;}DirectoryEntry
Bagian berjudul “DirectoryEntry”Merepresentasikan entri direktori.
export interface DirectoryEntry extends Entry { isFile: false; isDirectory: true;}ReadFileOptions
Bagian berjudul “ReadFileOptions”Opsi untuk membaca file.
export interface ReadFileOptions { /** Path to the file */ path: string; /** Base directory */ directory?: Directory; /** Encoding for text files (omit for binary/base64) */ encoding?: Encoding; /** Byte offset to start reading from (default: 0) */ offset?: number; /** Number of bytes to read (default: read to end of file) */ length?: number;}ReadFileResult
Bagian berjudul “ReadFileResult”Hasil pembacaan file.
export interface ReadFileResult { /** File contents as string (text) or base64 (binary) */ data: string;}WriteFileOptions
Bagian berjudul “WriteFileOptions”Opsi untuk menulis file.
export interface WriteFileOptions { /** Path to the file */ path: string; /** Base directory */ directory?: Directory; /** Data to write (string for text, base64 for binary) */ data: string; /** Encoding for text files */ encoding?: Encoding; /** If true, append to existing file instead of overwriting */ append?: boolean; /** Create intermediate directories if they don't exist */ recursive?: boolean; /** Byte position to start writing at (for random access writes). If not specified, writes from beginning or appends based on 'append' flag */ position?: number;}WriteFileResult
Bagian berjudul “WriteFileResult”Hasil dari penulisan file.
export interface WriteFileResult { /** The URI of the written file */ uri: string;}Sumber Kebenaran
Bagian berjudul “Sumber Kebenaran”Halaman ini dibuat dari plugin src/definitions.ts. Jalankan ulang sync saat API publik berubah di upstream.
Lanjutkan dari Getting Started
Bagian berjudul “Lanjutkan dari Getting Started”Jika Anda menggunakan Panduan Memulai untuk merencanakan penyimpanan dan penanganan file, hubungkan dengan Menggunakan @capgo/capacitor-file untuk kapabilitas native saat menggunakan @capgo/capacitor-file, @capgo/capacitor-data-storage-sqlite untuk detail implementasi di @capgo/capacitor-data-storage-sqlite, Menggunakan @capgo/capacitor-data-storage-sqlite untuk kapabilitas native di Menggunakan @capgo/capacitor-data-storage-sqlite, @capgo/capacitor-file untuk detail implementasi di @capgo/capacitor-file, dan @capgo/capacitor-uploader untuk detail implementasi di @capgo/capacitor-uploader.