开始
复制安装步骤和完整的markdown指南
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.
安装
安装您可以使用我们的AI辅助安装插件。将Capgo技能添加到您的AI工具中,使用以下命令:
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.如果您更喜欢手动设置,请通过运行以下命令安装插件并按照以下平台特定的说明进行操作:
bun add @capgo/capacitor-filebunx cap syncimport { CapacitorFile } from '@capgo/capacitor-file';API 概述
标题为“API 概述”的部分requestFileSystem
标题为“requestFileSystem”部分请求一个文件系统.
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.requestFileSystem({} as RequestFileSystemOptions);resolveLocalFileSystemURL
标题:resolveLocalFileSystemURL将文件 URL 解析为一个入口.
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.resolveLocalFileSystemURL({} as ResolveURLOptions);getFile
标题:getFile获取一个文件入口.
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.getFile({} as GetFileOptions);getDirectory
标题:getDirectory获取一个目录入口.
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.getDirectory({} as GetDirectoryOptions);readFile
标题:readFile读取文件为文本或base64。
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.readFile({} as ReadFileOptions);readAsDataURL
标题:readAsDataURL读取文件为数据URL(base64加 MIME类型前缀)
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.readAsDataURL({} as ReadFileOptions);writeFile
标题:writeFile将数据写入文件
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.writeFile({} as WriteFileOptions);appendFile
标题:appendFile将数据追加到文件
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.appendFile({} as WriteFileOptions);deleteFile
标题:deleteFile删除文件。
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.deleteFile({} as DeleteFileOptions);mkdir
mkdir标题创建目录。
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.mkdir({} as MkdirOptions);rmdir
rmdir标题删除目录。
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.rmdir({} as DeleteDirectoryOptions);readdir
readdir标题读取目录内容。
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.readdir({} as ReaddirOptions);stat
stat标题获取文件或目录的元数据。
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.stat({} as StatOptions);getMetadata
getMetadata获取文件或目录的元数据。 stat()的别名。
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.getMetadata({} as StatOptions);rename
rename重命名或移动文件或目录。
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.rename({} as RenameOptions);move
rename重命名或移动文件或目录。 rename()的别名。
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.move({} as RenameOptions);copy
rename复制文件或目录
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.copy({} as CopyOptions);exists
标题:exists检查文件或目录是否存在
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.exists({} as ExistsOptions);getUri
标题:getUri获取文件的URI
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.getUri({} as GetUriOptions);truncate
标题:truncate将文件截断到指定大小
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.truncate({} as TruncateOptions);getDirectories
标题:getDirectories获取所有已知的文件系统目录。
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.getDirectories();getFreeDiskSpace
getFreeDiskSpace获取可用磁盘空间(以字节为单位)。
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.getFreeDiskSpace();checkPermissions
checkPermissions检查文件操作的当前权限状态。 在 Android 上,这检查外部存储权限。 在 iOS 和 web 上,这始终返回 ‘已授权’,因为不需要特殊权限。
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.checkPermissions();requestPermissions
requestPermissions请求文件操作的权限。 在 Android 上,这请求外部存储权限以访问应用私有目录外的文件。 在 iOS 和 web 上,这始终返回 ‘已授权’,因为不需要特殊权限。
import { CapacitorFile } from '@capgo/capacitor-file';
await CapacitorFile.requestPermissions();类型参考
类型参考RequestFileSystemOptions
请求文件系统选项请求文件系统的选项
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
文件系统代表一个文件系统
export interface FileSystem { /** The name of the file system */ name: string; /** The root directory of the file system */ root: DirectoryEntry;}ResolveURLOptions
解析URL选项解析URL到入口的选项
export interface ResolveURLOptions { /** The URL to resolve (file:// or cdvfile://) */ url: string;}Entry
文件或目录入口代表一个文件或目录入口
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
标题:获取文件选项获取文件的选项。
export interface GetFileOptions { /** Path to the file */ path: string; /** Base directory */ directory?: Directory; /** Options for creating the file */ options?: GetOptions;}FileEntry
标题:文件条目表示文件条目。
export interface FileEntry extends Entry { isFile: true; isDirectory: false;}GetDirectoryOptions
标题:获取目录选项获取目录的选项。
export interface GetDirectoryOptions { /** Path to the directory */ path: string; /** Base directory */ directory?: Directory; /** Options for creating the directory */ options?: GetOptions;}DirectoryEntry
标题:目录条目表示目录条目。
export interface DirectoryEntry extends Entry { isFile: false; isDirectory: true;}ReadFileOptions
名为“ReadFileOptions”的部分读取文件的选项。
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
名为“ReadFileResult”的部分读取文件的结果。
export interface ReadFileResult { /** File contents as string (text) or base64 (binary) */ data: string;}WriteFileOptions
名为“WriteFileOptions”的部分写入文件的选项。
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
名为“WriteFileResult”的部分写入文件的结果。
export interface WriteFileResult { /** The URI of the written file */ uri: string;}真实数据来源
名为“真实数据来源”的部分此页面是由插件的 src/definitions.ts当公共 API 上游发生变化时,请重新运行同步。
从 Getting Started 继续
名为“从 Getting Started 继续”的部分如果您正在使用 Getting Started 来规划存储和文件处理,连接它到 使用 @capgo/capacitor-file 为在使用 @capgo/capacitor-file 中的本机功能的能力。 @capgo/capacitor-数据存储-sqlite 为 @capgo/capacitor-数据存储-sqlite 的实现细节 使用 @capgo/capacitor-数据存储-sqlite 为 @capgo/capacitor-数据存储-sqlite 的原生能力 @capgo/capacitor-文件 为 @capgo/capacitor-文件 的实现细节, 和 @capgo/capacitor-上传器 为 @capgo/capacitor-上传器 的实现细节。