@ capgo/ 电容文件
使用兼容 API 的 Capacitor app 设备文件系统工作 cordova-plugin-file。在iOS、Android和网络上读取、撰写、复制并移动文件和文件夹.
🤖 建筑与AI编码代理? 安装 Capgo 技能 所以克劳德代码,代码,光标,副驾驶,或双子座CLI知道如何安装和使用
@capgo/capacitor-file在您的电容应用程序中:npx skills add Cap-go/capgo-skills。然后问你的经纪人: 添加@capgo/capacor-file到我的Capacitor应用上并把它接上 代理设置指南 • 妇女 主机磁共振
主要特点
- 读写数字 :
readFile(), (中文(简体) ),readAsDataURL(), (中文(简体) ),writeFile(), (中文(简体) ),appendFile()财务报告和审定财务报表truncate()。 。 。 . - 文件与文件夹数字 :
mkdir(), (中文(简体) ),rmdir(), (中文(简体) ),readdir(), (中文(简体) ),copy(), (中文(简体) ),move(), (中文(简体) ),rename()财务报告和审定财务报表deleteFile()。 。 。 . - 元数据数字 :
stat(), (中文(简体) ),exists(), (中文(简体) ),getUri()财务报告和审定财务报表getFreeDiskSpace()。 。 。 . - Cordova 兼容数字 :
requestFileSystem(), (中文(简体) ),resolveLocalFileSystemURL(), (中文(简体) ),getFile()财务报告和审定财务报表getDirectory()方便从cordova-plugin-file。 。 。 . - 权限数字 :
checkPermissions()财务报告和审定财务报表requestPermissions()用于存储访问. - 平台: iOS,Android和Web. 互联网档案馆的存檔,存档日期2013-12-02. Web将核心文件操作保存在浏览器存储中.
为什么是控制器文件?
一个可靠的 本地文件系统 为 Capacitor 应用程序所建的插件, 具有官方文件系统插件中没有的高级特性 :
- 部分文件读取 - 读取文件的具体部分
offset财务报告和审定财务报表length参数( 大文件大) - 随机访问写入 - 在具体字节位置写作
position参数 - Cordova 兼容的 API - 熟悉的方法,比如:
requestFileSystem(), (中文(简体) ),resolveLocalFileSystemURL(), (中文(简体) ),getFile(),以及getDirectory() - 所有平台目录 - 查阅文件、图书馆、缓存、外部存储等
- 进展活动 - 通过实时更新进度,监测阅读/写入操作
- 自由和开放源代码 - 不需要有偿服务
适合需要与大文件合作,从Cordova迁移,或需要随机访问文件操作的应用程序.
文档
这里有最完整的医生: https://capgo.app/docs/plugins/files/ 互联网档案馆的存檔,存档日期2013-12-02
兼容性
| 插件版本 | 电容兼容性 | 维护 |
|---|---|---|
| 联合国 | 联合国 | ✅ |
| 页:1 | 页:1 | 应要求 |
| 页:1 | 页:1 | ❌ |
| 页:1 | 页:1 | ❌ |
说明: 此插件的主要版本沿用了Capacitor的主要版本. 使用匹配您的 Capacitor 安装的版本( 如 Capacitor 8 的插件 v8) 。 只有最新的主要版本得到积极维护.
安装
您可以使用我们的 AI 辅助设置安装插件 。 使用以下命令将 Capgo 技能添加到您的 AI 工具 :
npx skills add https://github.com/cap-go/capacitor-skills --skill capacitor-plugins
然后使用下列提示:
Use the `capacitor-plugins` skill from `cap-go/capacitor-skills` to install the `@capgo/capacitor-file` plugin in my project.
如果您更喜欢手动设置,请通过运行以下命令来安装插件,并遵循下面的平台特定指令:
npm install @capgo/capacitor-file
npx cap sync
Android 媒体权限和谷歌播放审查
在Android 13+上,此插件可以请求广泛的媒体权限,例如 READ_MEDIA_IMAGES 财务报告和审定财务报表 READ_MEDIA_VIDEO 当你打电话 requestPermissions() 在您的应用程序的私人目录之外访问 .
Google Play只允许这些相片和视频权限,当访问与你的app的核心目的直接相关,需要持续或频繁访问共享存储中的照片或视频. 如果您的应用只需要一次性或不频繁的媒体访问,例如导入一个配置图片,选择附件,或者选择一个文件上传,Google期望您使用一个系统拾取器代替.
在发布请求这些权限的应用程序之前 :
- 确保照片或视频库的访问是您的应用程序核心功能的一部分.
- 准备好为Play Console审查中的许可使用说明理由.
- 如果应用程序只能与用户选择的文件合作,则删除宽媒体权限 .
如果 Google Play 审查以类似消息拒绝应用程序 Photo and Video Permissions policy: Permission use is not directly related to your app's core purpose,使用时 @ capgo/ 电容器- 文件拾取器 或另一个系统采取器流,而不是请求宽相/视频权限.
见谷歌 图片和视频权限政策 目前审查所需经费.
API 密码
requestFileSystem(...)resolveLocalFileSystemURL(...)getFile(...)getDirectory(...)readFile(...)readAsDataURL(...)writeFile(...)appendFile(...)deleteFile(...)mkdir(...)rmdir(...)readdir(...)stat(...)getMetadata(...)rename(...)move(...)copy(...)exists(...)getUri(...)truncate(...)getDirectories()getFreeDiskSpace()addListener('readProgress', ...)addListener('writeProgress', ...)removeAllListeners()getPluginVersion()checkPermissions()requestPermissions(...)- 接口
- 类型异名
- 数字
电容文件插件 执行类似 Cordova 文件插件的文件系统操作
请求FileSystem (...)
requestFileSystem(options: RequestFileSystemOptions) => Promise<FileSystem>
请求文件系统 .
| 参数 | 类型 | 说明 |
|---|---|---|
options | RequestFileSystemOptions | - 文件系统请求选项 |
回返情况: Promise<FileSystem>
解析本地文件SystemURL(...)
resolveLocalFileSystemURL(options: ResolveURLOptions) => Promise<Entry>
将文件 URL 解析为条目 .
| 参数 | 类型 | 说明 |
|---|---|---|
options | ResolveURLOptions | - 要解决的 URL |
回返情况: Promise<Entry>
获取文件(...)
getFile(options: GetFileOptions) => Promise<FileEntry>
获取文件条目 .
| 参数 | 类型 | 说明 |
|---|---|---|
options | GetFileOptions | - 文件路径和选项 |
回返情况: Promise<FileEntry>
获取 Directy (...)
getDirectory(options: GetDirectoryOptions) => Promise<DirectoryEntry>
获取目录条目 .
| 参数 | 类型 | 说明 |
|---|---|---|
options | GetDirectoryOptions | - 说吧 目录 路径和选项 |
回返情况: Promise<DirectoryEntry>
读取文件(...)
readFile(options: ReadFileOptions) => Promise<ReadFileResult>
读取文件为文本或碱基64.
| 参数 | 类型 | 说明 |
|---|---|---|
options | ReadFileOptions | - 读取选项 |
回返情况: Promise<ReadFileResult>
读取 AsDataURL(...)
readAsDataURL(options: ReadFileOptions) => Promise<{ data: string; }>
将一个文件读取为数据 URL(base64 有 MIME 类型前缀).
| 参数 | 类型 | 说明 |
|---|---|---|
options | ReadFileOptions | - 读取选项 |
回返情况: Promise<{ data: string; }>
写入文件(...)
writeFile(options: WriteFileOptions) => Promise<WriteFileResult>
将数据写入文件 .
| 参数 | 类型 | 说明 |
|---|---|---|
options | WriteFileOptions | - 写选项 |
回返情况: Promise<WriteFileResult>
附加文件(...)
appendFile(options: WriteFileOptions) => Promise<WriteFileResult>
将数据添加到文件中 .
| 参数 | 类型 | 说明 |
|---|---|---|
options | WriteFileOptions | - 写选项( 附录被迫为真) |
回返情况: Promise<WriteFileResult>
删除文件(...)
deleteFile(options: DeleteFileOptions) => Promise<void>
删除文件 .
| 参数 | 类型 | 说明 |
|---|---|---|
options | DeleteFileOptions | - 删除选项 |
mkdir (...) (中文(简体) )
mkdir(options: MkdirOptions) => Promise<void>
创建目录 .
| 参数 | 类型 | 说明 |
|---|---|---|
options | MkdirOptions | - 说吧 目录 创建选项 |
rmdir (...) (中文(简体) )
rmdir(options: DeleteDirectoryOptions) => Promise<void>
删除目录 .
| 参数 | 类型 | 说明 |
|---|---|---|
options | DeleteDirectoryOptions | - 删除选项 |
已读 dir (...)
readdir(options: ReaddirOptions) => Promise<ReaddirResult>
读取目录内容 .
| 参数 | 类型 | 说明 |
|---|---|---|
options | ReaddirOptions | - 读取选项 |
回返情况: Promise<ReaddirResult>
数据(...)
stat(options: StatOptions) => Promise<StatResult>
获取文件或目录的元数据 .
| 参数 | 类型 | 说明 |
|---|---|---|
options | StatOptions | - Stat 选项 |
回返情况: Promise<StatResult>
获取气象数据(...)
getMetadata(options: StatOptions) => Promise<Metadata>
获取文件或目录的元数据 . 别名为 stat ().
| 参数 | 类型 | 说明 |
|---|---|---|
options | StatOptions | - Stat 选项 |
回返情况: Promise<Metadata>
重命名(...)
rename(options: RenameOptions) => Promise<void>
重命名或移动文件或目录.
| 参数 | 类型 | 说明 |
|---|---|---|
options | RenameOptions | - 重命名选项 |
移动(...)
move(options: RenameOptions) => Promise<void>
移动文件或目录 . 重命名的别名 ().
| 参数 | 类型 | 说明 |
|---|---|---|
options | RenameOptions | - 移动选项 |
复制(...)
copy(options: CopyOptions) => Promise<CopyResult>
复制文件或目录 .
| 参数 | 类型 | 说明 |
|---|---|---|
options | CopyOptions | - 复制选项 |
回返情况: Promise<CopyResult>
已存在(...)
exists(options: ExistsOptions) => Promise<ExistsResult>
检查文件或目录是否存在 .
| 参数 | 类型 | 说明 |
|---|---|---|
options | ExistsOptions | - 检查选项 |
回返情况: Promise<ExistsResult>
获取Uri(...)
getUri(options: GetUriOptions) => Promise<GetUriResult>
获取URI文件 .
| 参数 | 类型 | 说明 |
|---|---|---|
options | GetUriOptions | - URI选项 |
回返情况: Promise<GetUriResult>
切变(...)
truncate(options: TruncateOptions) => Promise<void>
将文件截断到指定大小.
| 参数 | 类型 | 说明 |
|---|---|---|
options | TruncateOptions | - 截断选项 |
获取董事( )
getDirectories() => Promise<FileDirectories>
获取所有已知的文件系统目录 .
回返情况: Promise<FileDirectories>
获取 FreeDisk Space ()
getFreeDiskSpace() => Promise<{ free: number; }>
在字节中获取自由磁盘空间 .
回返情况: Promise<{ free: number; }>
添加倾听器( 读取进度 )..
addListener(eventName: 'readProgress', listenerFunc: (progress: ProgressEvent) => void) => Promise<PluginListenerHandle>
听读进度事件.
| 参数 | 类型 | 说明 |
|---|---|---|
eventName | 'readProgress' | - 一定是"读取进步" |
listenerFunc | (progress: ProgressEvent) => void | - 召回接收进度更新 |
回返情况: Promise<PluginListenerHandle>
添加倾听器( “ 写入进度 ” ,...)
addListener(eventName: 'writeProgress', listenerFunc: (progress: ProgressEvent) => void) => Promise<PluginListenerHandle>
听写进度事件.
| 参数 | 类型 | 说明 |
|---|---|---|
eventName | 'writeProgress' | - 一定是"写进步" |
listenerFunc | (progress: ProgressEvent) => void | - 召回接收进度更新 |
回返情况: Promise<PluginListenerHandle>
删除所有收听器( )
removeAllListeners() => Promise<void>
删除全部事件听众 .
获取 PluginVersion ()
getPluginVersion() => Promise<{ version: string; }>
获取插件版本 .
回返情况: Promise<{ version: string; }>
检查( )
checkPermissions() => Promise<FilePermissionStatus>
请检查当前文件操作的权限状态 . 在Android上,此检查外部存储权限. 在iOS和网络上,这总是返回"被授予",因为不需要特殊权限.
回返情况: Promise<FilePermissionStatus>
请求(...)
requestPermissions(options?: PermissionRequestOptions | undefined) => Promise<FilePermissionStatus>
请求文件操作权限 . 在Android上,此请求需要外部存储权限 访问应用程序的私人目录以外的文件 . 在iOS和网络上,这总是返回"被授予",因为不需要特殊权限.
| 参数 | 类型 | 说明 |
|---|---|---|
options | PermissionRequestOptions | - 许可请求的可选配置 |
回返情况: Promise<FilePermissionStatus>
接口
文件系统
代表一个文件系统
| Prop | 类型 | 说明 |
|---|---|---|
name | string | 文件系统的名称 |
root | DirectoryEntry | 文件系统的根目录 |
目录条目
代表目录条目
| Prop | 类型 | 说明 |
|---|---|---|
isFile | false | 如果这是文件, 则为真 |
isDirectory | true | 如果此目录为真 |
请求文件系统选项
请求文件系统的选项
| Prop | 类型 | 说明 |
|---|---|---|
type | FileSystemType | 要请求的文件系统类型 |
size | number | 请求的字节大小( 可能不会在所有平台上执行) |
条目
代表一个文件或目录条目
| Prop | 类型 | 说明 |
|---|---|---|
isFile | boolean | 如果这是文件, 则为真 |
isDirectory | boolean | 如果此目录为真 |
name | string | 文件或目录的名称 |
fullPath | string | 相对于文件系统根的完整路径 |
nativeURL | string | 原生档案 : URI 资源 |
选项
解决 URL 到条目的选项
| Prop | 类型 | 说明 |
|---|---|---|
url | string | 要解决的 URL (file:// 或 cdvfile://) |
文件条目
代表文件条目
| Prop | 类型 | 说明 |
|---|---|---|
isFile | true | 如果这是文件, 则为真 |
isDirectory | false | 如果此目录为真 |
获取文件选项
获取文件的选项
| Prop | 类型 | 说明 |
|---|---|---|
path | string | 文件的路径 |
directory | Directory | 基准目录 |
options | GetOptions | 创建文件的选项 |
获取选项
创建或获取文件/目录的选项
| Prop | 类型 | 说明 |
|---|---|---|
create | boolean | 如果不存在, 则创建文件/ 目录 |
exclusive | boolean | 如果真实和创建是真实的, 如果文件/ 目录已经存在, 丢出错误 |
获取切换选项
获取目录的选项
| Prop | 类型 | 说明 |
|---|---|---|
path | string | 目录的路径 |
directory | Directory | 基准目录 |
options | GetOptions | 创建目录的选项 |
读取结果
读取文件的结果
| Prop | 类型 | 说明 |
|---|---|---|
data | string | 文件内容为字符串(文本)或基数64(二进制) |
读取文件选项
读取文件的选项
| Prop | 类型 | 说明 |
|---|---|---|
path | string | 文件的路径 |
directory | Directory | 基准目录 |
encoding | Encoding | 编码 文本文件(二进制/基准64的缩写) |
offset | number | 开始读取的字节偏移( 默认 : 0) |
length | number | 要读取的字节数( 默认: 已读到文件结尾) |
写入文件
编写文件的结果
| Prop | 类型 | 说明 |
|---|---|---|
uri | string | 写入文件的 URI |
写入文件选项
写入文件的选项
| Prop | 类型 | 说明 |
|---|---|---|
path | string | 文件的路径 |
directory | Directory | 基准目录 |
data | string | 要写入的数据( 字符串用于文本, 基数64用于二进制) |
encoding | Encoding | 编码 用于文本文件 |
append | boolean | 如果为真, 请将附件附加到已有文件, 而不是覆盖 |
recursive | boolean | 如果中间目录不存在, 则创建此目录 |
position | number | 开始写入的字节位置( 对于随机访问写入) 。 如果未指定, 则在“ 附录” 旗下从首字母写入或附加 |
删除文件选项
删除文件或目录的选项
| Prop | 类型 | 说明 |
|---|---|---|
path | string | 文件或目录的路径 |
directory | Directory | 基准目录 |
Mkdir 选项
创建目录的选项
| Prop | 类型 | 说明 |
|---|---|---|
path | string | 目录的路径 |
directory | Directory | 基准目录 |
recursive | boolean | 如果中间目录不存在, 则创建此目录 |
删除标题选项
递归删除目录的选项
| Prop | 类型 | 说明 |
|---|---|---|
path | string | 目录的路径 |
directory | Directory | 基准目录 |
recursive | boolean | 如果为真, 则连续删除内容 |
读取结果
读取目录的结果
| Prop | 类型 | 说明 |
|---|---|---|
entries | Entry[] | 目录中的条目列表 |
读取目录选项
读取目录的选项
| Prop | 类型 | 说明 |
|---|---|---|
path | string | 目录的路径 |
directory | Directory | 基准目录 |
静态
获得文件/目录信息的结果
| Prop | 类型 | 说明 |
|---|---|---|
type | 'file' | 'directory' | 类型 : “ 文件” 或“ 目录 ” |
size | number | 以字节表示的大小 |
ctime | number | 创作时间(如果有的话) |
mtime | number | 上次修改时间 |
uri | string | 乌干达共和国 |
状态选项
获取文件信息的选项
| Prop | 类型 | 说明 |
|---|---|---|
path | string | 文件或目录的路径 |
directory | Directory | 基准目录 |
元数据
代表文件或目录的元数据
| Prop | 类型 | 说明 |
|---|---|---|
modificationTime | number | 上次修改日期 |
size | number | 以字节表示的大小(目录为0) |
重命名选项
重命名/移动文件或目录的选项
| Prop | 类型 | 说明 |
|---|---|---|
from | string | 当前路径 |
to | string | 新路径 |
directory | Directory | “ 从” 路径的基准目录 |
toDirectory | Directory | “ to” 路径的基础目录 |
复制结果
复制操作结果
| Prop | 类型 | 说明 |
|---|---|---|
uri | string | 复制文件的 URI |
复制选项
复制文件或目录的选项
| Prop | 类型 | 说明 |
|---|---|---|
from | string | 源路径 |
to | string | 目标路径 |
directory | Directory | 源代码基础目录 |
toDirectory | Directory | 目的地基准目录 |
现成
存在结果检查
| Prop | 类型 | 说明 |
|---|---|---|
exists | boolean | 如果文件或目录存在, 则为真 |
type | 'file' | 'directory' | 类型( 如果存在): “ 文件” 或“ 目录 ” |
已存在选项
检查文件或目录是否存在的选项
| Prop | 类型 | 说明 |
|---|---|---|
path | string | 检查路径 |
directory | Directory | 基准目录 |
GetUriResult (英语)
获得URI的结果
| Prop | 类型 | 说明 |
|---|---|---|
uri | string | 本地的URI公司 |
获取选项
获取文件 URI 选项
| Prop | 类型 | 说明 |
|---|---|---|
path | string | 文件的路径 |
directory | Directory | 基准目录 |
截断选项
文件切换选项
| Prop | 类型 | 说明 |
|---|---|---|
path | string | 文件的路径 |
directory | Directory | 基准目录 |
size | number | 缩写为( 默认为 0) |
文件目录
插件曝光的已知文件系统目录
| Prop | 类型 | 说明 |
|---|---|---|
applicationDirectory | string | 应用程序安装目录( 只读) |
applicationStorageDirectory | string | 应用程序存储目录根 |
dataDirectory | string | 持续私有数据目录 |
cacheDirectory | string | 缓存目录 |
externalRootDirectory | string | 外部根目录 (Android) |
externalApplicationStorageDirectory | string | 外部应用程序存储目录 (Android) |
externalDataDirectory | string | 外部数据目录 (Android) |
externalCacheDirectory | string | 外部缓存目录 (Android) |
documentsDirectory | string | 文件目录( iOS) |
syncedDataDirectory | string | 同步数据目录( iOS iCloud) |
libraryDirectory | string | 图书馆目录(iOS) |
tempDirectory | string | 临时目录 |
插件听筒
| Prop | 类型 |
|---|---|
remove | () => Promise<void> |
进展活动
文件操作进度事件
| Prop | 类型 | 说明 |
|---|---|---|
loaded | number | 迄今已装入的字节 |
total | number | 总字节( 如果已知) |
lengthComputable | boolean | 总数是否可计算 |
文件发送状态
文件操作的权限状态
| Prop | 类型 | 说明 |
|---|---|---|
publicStorage | PermissionState | 读取公共/ 外部存储的权限状态 |
权限请求选项
请求权限的选项
| Prop | 类型 | 说明 | 默认 |
|---|---|---|---|
showSettingsAlert | boolean | 如果为真, 在拒绝许可时显示对打开设置的提醒 | false |
title | string | 设置提醒对话框的标题 | |
message | string | 设置提醒对话框的消息 | |
openSettingsButtonTitle | string | 打开设置按钮的文本 | |
cancelButtonTitle | string | “取消”按钮的文本 |
类型异名
权限状态
'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'
数字
文件系统Type
| 成员 | 数值 |
|---|---|
TEMPORARY | 0 |
PERSISTENT | 1 |
目录
| 成员 | 数值 | 说明 |
|---|---|---|
Documents | 'DOCUMENTS' | 文件目录(iOS)/文件目录(Android) 持久性,用户可见,备份 |
Data | 'DATA' | 数据目录 持久性私有数据存储 |
Library | 'LIBRARY' | 图书馆目录(iOS)/文件目录(Android) 持续但用户看不见 |
Cache | 'CACHE' | 缓存目录 临时缓存, 可能由 OS 清除 |
External | 'EXTERNAL' | 外部存储( 只有Android) SD 卡或外部存储根 |
ExternalStorage | 'EXTERNAL_STORAGE' | 外部存储数据目录( 只有Android) |
Application | 'APPLICATION' | 应用程序捆绑/资产目录( 只读) |
编码
| 成员 | 数值 |
|---|---|
UTF8 | 'utf8' |
ASCII | 'ascii' |
UTF16 | 'utf16' |
用法实例
正在读取文件
import { CapacitorFile, Directory, Encoding } from '@capgo/capacitor-file';
// Read as text
const result = await CapacitorFile.readFile({
path: 'my-file.txt',
directory: Directory.Documents,
encoding: Encoding.UTF8,
});
console.log(result.data);
// Read as base64 (binary)
const binaryResult = await CapacitorFile.readFile({
path: 'image.png',
directory: Directory.Documents,
});
console.log(binaryResult.data); // base64 string
// Read partial file (offset and length) - great for large files!
const partialResult = await CapacitorFile.readFile({
path: 'large-file.bin',
directory: Directory.Documents,
offset: 1024, // Start at byte 1024
length: 512, // Read 512 bytes
});
console.log(partialResult.data); // base64 encoded chunk
写入文件
import { CapacitorFile, Directory, Encoding } from '@capgo/capacitor-file';
// Write text
await CapacitorFile.writeFile({
path: 'my-file.txt',
directory: Directory.Documents,
data: 'Hello, World!',
encoding: Encoding.UTF8,
recursive: true, // Create parent directories if needed
});
// Write binary (base64)
await CapacitorFile.writeFile({
path: 'image.png',
directory: Directory.Documents,
data: 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==',
});
// Write at specific position (random access) - great for patching files!
await CapacitorFile.writeFile({
path: 'data.bin',
directory: Directory.Documents,
data: 'PATCHED',
encoding: Encoding.UTF8,
position: 100, // Write starting at byte 100
});
创建和阅读目录
import { CapacitorFile, Directory } from '@capgo/capacitor-file';
// Create a directory
await CapacitorFile.mkdir({
path: 'my-folder/sub-folder',
directory: Directory.Documents,
recursive: true,
});
// Read directory contents
const result = await CapacitorFile.readdir({
path: 'my-folder',
directory: Directory.Documents,
});
for (const entry of result.entries) {
console.log(`${entry.name} - ${entry.isFile ? 'file' : 'directory'}`);
}
复制和移动文件
import { CapacitorFile, Directory } from '@capgo/capacitor-file';
// Copy a file
await CapacitorFile.copy({
from: 'original.txt',
to: 'backup/original-copy.txt',
directory: Directory.Documents,
toDirectory: Directory.Documents,
});
// Move/rename a file
await CapacitorFile.rename({
from: 'old-name.txt',
to: 'new-name.txt',
directory: Directory.Documents,
});
检查文件存在
import { CapacitorFile, Directory } from '@capgo/capacitor-file';
const result = await CapacitorFile.exists({
path: 'my-file.txt',
directory: Directory.Documents,
});
if (result.exists) {
console.log(`Found ${result.type}`);
}
获取文件信息
import { CapacitorFile, Directory } from '@capgo/capacitor-file';
const stat = await CapacitorFile.stat({
path: 'my-file.txt',
directory: Directory.Documents,
});
console.log(`Size: ${stat.size} bytes`);
console.log(`Modified: ${new Date(stat.mtime)}`);
console.log(`Type: ${stat.type}`);
从 cordova-plugin- file 迁移
此插件提供类似的功能至 cordova-plugin-file 还有更现代的API. 简而言之:
| Cordova 文件插件 | 此插件 |
|---|---|
window.resolveLocalFileSystemURL() | CapacitorFile.resolveLocalFileSystemURL() |
directoryEntry.getFile() | CapacitorFile.getFile() |
directoryEntry.getDirectory() | CapacitorFile.getDirectory() |
fileReader.readAsText() | CapacitorFile.readFile({ encoding: Encoding.UTF8 }) |
fileReader.readAsDataURL() | CapacitorFile.readAsDataURL() |
fileWriter.write() | CapacitorFile.writeFile() |
entry.remove() | CapacitorFile.deleteFile() |
entry.moveTo() | CapacitorFile.move() |
entry.copyTo() | CapacitorFile.copy() |
directoryEntry.removeRecursively() | CapacitorFile.rmdir({ recursive: true }) |
cordova.file.dataDirectory | Directory.Data |
cordova.file.documentsDirectory | Directory.Documents |
cordova.file.cacheDirectory | Directory.Cache |
许可证
MPL - 2.0 导弹