メインコンテンツへスキップ

スタートガイド

GitHub

AIアシストによるセットアップでプラグインをインストールできます。以下のコマンドを使用して、お使いのAIツールに Capgo のスキルを追加してください:

ターミナル
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

次に、以下のプロンプトを使用します:

Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-file` plugin in my project.

手動セットアップを希望する場合は、以下のコマンドを実行してプラグインをインストールし、下記のプラットフォーム別の手順に従ってください:

ターミナル
npm install @capgo/capacitor-file
npx cap sync
import { CapacitorFile } from '@capgo/capacitor-file';
import { CapacitorFile, FileSystemType } from '@capgo/capacitor-file';
const result = await CapacitorFile.requestFileSystem({ type: FileSystemType.TEMPORARY });
console.log(result);
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);
import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getFile({ path: 'path/to/file' });
console.log(result);
  • 権限:呼び出し checkPermissions() その後 requestPermissions() 必要に応じて。
  • リスナー: 画面がアンマウントされたらハンドルを削除します。
  • 信頼できる情報源: 信頼 src/definitions.ts GitHub リポジトリ内。

ファイルシステムをリクエストします。

import { CapacitorFile, FileSystemType } from '@capgo/capacitor-file';
const result = await CapacitorFile.requestFileSystem({ type: FileSystemType.TEMPORARY });
console.log(result);

ファイルURLをエントリーに解決します。

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.resolveLocalFileSystemURL({ url: 'https://example.com' });
console.log(result);

ファイルエントリーを取得します。

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getFile({ path: 'path/to/file' });
console.log(result);

ディレクトリエントリを取得します。

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getDirectory({ path: 'path/to/file' });
console.log(result);

ファイルをテキストまたはbase64として読み込みます。

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.readFile({ path: 'path/to/file' });
console.log(result);

ファイルをData URL(MIMEタイプのプレフィックス付きbase64)として読み込みます。

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.readAsDataURL({ path: 'path/to/file' });
console.log(result);

ファイルにデータを書き込みます。

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.writeFile({
path: 'path/to/file',
data: 'data',
});
console.log(result);

ファイルにデータを追記します。

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.appendFile({
path: 'path/to/file',
data: 'data',
});
console.log(result);

ファイルを削除します。

import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.deleteFile({ path: 'path/to/file' });

ディレクトリを作成します。

import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.mkdir({ path: 'path/to/file' });

ディレクトリを削除します。

import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.rmdir({ path: 'path/to/file' });

ディレクトリの内容を読み取ります。

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.readdir({ path: 'path/to/file' });
console.log(result);

ファイルまたはディレクトリのメタデータを取得します。

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.stat({ path: 'path/to/file' });
console.log(result);

ファイルまたはディレクトリのメタデータを取得します。 stat() のエイリアスです。

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getMetadata({ path: 'path/to/file' });
console.log(result);

ファイルまたはディレクトリの名前を変更または移動します。

import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.rename({
from: 'from',
to: 'to',
});

ファイルまたはディレクトリを移動します。 rename() のエイリアスです。

import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.move({
from: 'from',
to: 'to',
});

ファイルまたはディレクトリをコピーします。

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.copy({
from: 'from',
to: 'to',
});
console.log(result);

ファイルまたはディレクトリが存在するか確認します。

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.exists({ path: 'path/to/file' });
console.log(result);

ファイルのURIを取得します。

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getUri({ path: 'path/to/file' });
console.log(result);

ファイルを指定したサイズに切り詰めます。

import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.truncate({ path: 'path/to/file' });

既知のファイルシステムディレクトリをすべて取得します。

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getDirectories();
console.log(result);

空きディスク容量をバイト単位で取得します。

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.getFreeDiskSpace();
console.log(result);

ファイル操作の現在の権限ステータスを確認します。 Androidでは外部ストレージの権限を確認します。 iOSとWebでは、特別な権限が不要なため、常に 'granted' を返します。

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.checkPermissions();
console.log(result);

ファイル操作の権限をリクエストします。 Androidでは、アプリのプライベートディレクトリ以外の ファイルにアクセスするために必要な外部ストレージ権限をリクエストします。 iOSおよびWebでは、特別な権限は不要なため、常に 'granted' を返します。

import { CapacitorFile } from '@capgo/capacitor-file';
const result = await CapacitorFile.requestPermissions();
console.log(result);

ファイルシステムの取得オプション。

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;
}

ファイルシステムを表します。

export interface FileSystem {
/** The name of the file system */
name: string;
/** The root directory of the file system */
root: DirectoryEntry;
}

URLをエントリーに解決するためのオプション。

export interface ResolveURLOptions {
/** The URL to resolve (file:// or cdvfile://) */
url: string;
}

ファイルまたはディレクトリのエントリーを表します。

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;
}

ファイルを取得するためのオプション。

export interface GetFileOptions {
/** Path to the file */
path: string;
/** Base directory */
directory?: Directory;
/** Options for creating the file */
options?: GetOptions;
}

ファイルのエントリーを表します。

export interface FileEntry extends Entry {
isFile: true;
isDirectory: false;
}

ディレクトリ取得のオプション

export interface GetDirectoryOptions {
/** Path to the directory */
path: string;
/** Base directory */
directory?: Directory;
/** Options for creating the directory */
options?: GetOptions;
}

ディレクトリエントリを表します。

export interface DirectoryEntry extends Entry {
isFile: false;
isDirectory: true;
}

ファイル読み込みのオプション

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;
}

ファイル読み込みの結果

export interface ReadFileResult {
/** File contents as string (text) or base64 (binary) */
data: string;
}

ファイルの書き込みオプション

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;
}

ファイルの書き込み結果

export interface WriteFileResult {
/** The URI of the written file */
uri: string;
}

このページはプラグインの src/definitions.tsから生成されています。upstream で公開 API が変更されたら、再度 sync を実行してください。

Getting Startedの続きを進める

「Getting Started からの続き」セクション

使用している場合 スタートガイド ストレージとファイル処理を計画するには、以下と連携してください: @capgo/capacitor-file の使い方 @capgo/capacitor-file を使用して Native Build の機能を利用する場合、 @capgo/capacitor-data-storage-sqlite @capgo/capacitor-data-storage-sqlite の実装の詳細については、 @capgo/capacitor-data-storage-sqlite の使い方 「@capgo/capacitor-data-storage-sqliteの使用方法」におけるネイティブ機能について、 @capgo/capacitor-file @capgo/capacitor-file の実装の詳細については、 @capgo/capacitor-uploader @capgo/capacitor-uploader. の実装の詳細については