@capacitor/filesystem
文件系统 API 提供了类似 NodeJS 的 API,用于在设备上处理文件。
安装
npm install @capacitor/filesystem
npx cap sync
Apple 隐私清单要求
Apple 要求应用开发者现在必须指定 API 使用批准理由以增强用户隐私。到 2024 年 5 月 1 日,向 App Store Connect 提交应用时必须包含这些理由。
在您的应用中使用此特定插件时,您必须在 /ios/App 中创建 PrivacyInfo.xcprivacy 文件或使用 VS Code 扩展生成它,指定使用理由。
有关如何执行此操作的详细步骤,请参阅 Capacitor 文档。
对于此插件,必需的字典键是 NSPrivacyAccessedAPICategoryFileTimestamp,推荐的理由是 C617.1。
PrivacyInfo.xcprivacy 示例
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSPrivacyAccessedAPICategoryFileTimestamp</key>
<array>
<dict>
<key>NSPrivacyAccessedAPIType</key>
<string>C617.1</string>
<key>NSPrivacyAccessedAPIReasons</key>
<array>
<string>CA92.1</string>
</array>
</dict>
</array>
</dict>
</plist>
目录
API
readFile(...)
readFile(options: ReadFileOptions) => Promise<ReadFileResult>
读取文件到内存中
| Param | Type |
|---|---|
options | |
Returns:
Promise<ReadFileResult>
Since: 1.0.0
writeFile(...)
writeFile(options: WriteFileOptions) Promise<void>
将数据写入文件
| Param | Type |
|---|---|
options | |
Since: 1.0.0
appendFile(...)
appendFile(options: AppendFileOptions) => Promise<void>
将数据追加到文件
| Param | Type |
|---|---|
options | |
Since: 1.0.0
deleteFile(...)
deleteFile(options: DeleteFileOptions) => Promise<void>
删除文件
| Param | Type |
|---|---|
options | |
Since: 1.0.0
mkdir(...)
mkdir(options: MkdirOptions) => Promise<void>
创建目录
| Param | Type |
|---|---|
options | |
Since: 1.0.0
rmdir(...)
rmdir(options: RmdirOptions) => Promise<void>
删除目录
| Param | Type |
|---|---|
options | |
Since: 1.0.0
readdir(...)
readdir(options: ReaddirOptions) => Promise<ReaddirResult>
返回目录中的文件和目录列表
| Param | Type |
|---|---|
options | |
Returns:
Promise<ReaddirResult>
Since: 1.0.0
getUri(...)
getUri(options: GetUriOptions) => Promise<GetUriResult>
获取文件 URI
| Param | Type |
|---|---|
options | |
Returns:
Promise<GetUriResult>
Since: 1.0.0
stat(...)
stat(options: StatOptions) => Promise<StatResult>
返回文件或目录元数据对象
| Param | Type |
|---|---|
options | |
Returns:
Promise<StatResult>
Since: 1.0.0
rename(...)
rename(options: RenameOptions) => Promise<void>
重命名文件或目录
| Param | Type |
|---|---|
options | |
Since: 1.0.0
copy(...)
copy(options: CopyOptions) => Promise<void>
将文件或目录复制到新路径
| Param | Type |
|---|---|
options | |
Since: 1.0.0
Interfaces
ReadFileOptions
| Prop | Type | Description | Default | Since |
|---|---|---|---|---|
path | string | 文件或目录的路径 | 1.0.0 | |
directory | | 目录操作 | 1.0.0 | |
encoding | | 编码,默认为 utf8 | utf8 | 1.0.0 |
ReadFileResult
| Prop | Type | Description | Since |
|---|---|---|---|
data | string | 文件的数据 | 1.0.0 |
WriteFileOptions
| Prop | Type | Description | Default | Since |
|---|---|---|---|---|
path | string | 文件或目录的路径 | 1.0.0 | |
data | string | 要写入的数据 | 1.0.0 | |
directory | | 目录操作 | 1.0.0 | |
encoding | | 编码,默认为 utf8 | utf8 | 1.0.0 |
recursive | boolean | 如果路径不存在则创建 | false | 1.0.0 |
AppendFileOptions
| Prop | Type | Description | Default | Since |
|---|---|---|---|---|
path | string | 文件或目录的路径 | 1.0.0 | |
data | string | 要追加的数据 | 1.0.0 | |
directory | | 目录操作 | 1.0.0 | |
encoding | | 编码,默认为 utf8 | utf8 | 1.0.0 |
DeleteFileOptions
| Prop | Type | Description | Default | Since |
|---|---|---|---|---|
path | string | 文件的路径 | 1.0.0 | |
directory | | 目录操作 | 1.0.0 |
MkdirOptions
| Prop | Type | Description | Default | Since |
|---|---|---|---|---|
path | string | 要创建的目录的路径 | 1.0.0 | |
directory | | 目录操作 | 1.0.0 | |
recursive | boolean | 如果路径不存在则创建 | false | 1.0.0 |
RmdirOptions
| Prop | Type | Description | Default | Since |
|---|---|---|---|---|
path | string | 目录的路径 | 1.0.0 | |
directory | | 目录操作 | 1.0.0 | |
recursive | boolean | 如果目录非空则递归删除 | false | 1.0.0 |
ReaddirOptions
| Prop | Type | Description | Default | Since |
|---|---|---|---|---|
path | string | 目录的路径 | 1.0.0 | |
directory | | 目录操作 | 1.0.0 |
ReaddirResult
| Prop | Type | Description | Since |
|---|---|---|---|
files | string[] | 目录中的文件和目录名称数组 | 1.0.0 |
GetUriOptions
| Prop | Type | Description | Default | Since |
|---|---|---|---|---|
path | string | 文件的路径 | 1.0.0 | |
directory | | 目录操作 | 1.0.0 |
GetUriResult
| Prop | Type | Description | Since |
|---|---|---|---|
uri | string | 文件的 URI 表示 | 1.0.0 |
StatOptions
| Prop | Type | Description | Default | Since |
|---|---|---|---|---|
path | string | 文件的路径 | 1.0.0 | |
directory | | 目录操作 | 1.0.0 |
StatResult
| Prop | Type | Description | Since |
|---|---|---|---|
type | string | 文件类型 | 1.0.0 |
size | number | 文件大小(字节) | 1.0.0 |
ctime | number | 创建时间(Unix 时间戳) | 1.0.0 |
mtime | number | 修改时间(Unix 时间戳) | 1.0.0 |
uri | string | 文件的 URI 表示 | 1.0.0 |
RenameOptions
| Prop | Type | Description | Since |
|---|---|---|---|
from | string | 文件或目录的当前路径 | 1.0.0 |
to | string | 文件或目录的新路径 | 1.0.0 |
directory | | 目录操作 | 1.0.0 |
CopyOptions
| Prop | Type | Description | Since |
|---|---|---|---|
from | string | 文件或目录的当前路径 | 1.0.0 |
to | string | 文件或目录的目标路径 | 1.0.0 |
directory | | 目录操作 | 1.0.0 |
Enums
Directory
| Members | Value | Description | Since |
|---|---|---|---|
Documents | 'DOCUMENTS' | Documents 目录 | 1.0.0 |
Data | 'DATA' | Data 目录 | 1.0.0 |
Cache | 'CACHE' | Cache 目录 | 1.0.0 |
External | 'EXTERNAL' | External 目录 | 1.0.0 |
ExternalStorage | 'EXTERNAL_STORAGE' | External Storage 目录 | 1.0.0 |
Encoding
| Members | Value | Description | Since |
|---|---|---|---|
UTF8 | 'utf8' | 1.0.0 | |
ASCII | 'ascii' | 1.0.0 | |
UTF16 | 'utf16' | 1.0.0 |