readWriteFileInApp
App 端文件读写适配器,被 showFile 内部调用。
前置依赖
typescript
interface FileInAppDeps {
getFileSystemManager: UniApp['getFileSystemManager']
log: Console['log']
error: Console['error']
}| 依赖 | 说明 |
|---|---|
getFileSystemManager | 获取 UniApp 文件系统管理器 |
log / error | 成功 / 失败日志 |
readFileInApp
函数签名
typescript
function readFileInApp(options: FileOptions, deps: FileInAppDeps): Promise<FileResult>参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
options.filePath | string | 是 | 本地文件路径 |
options.encoding | string | 否 | 编码,默认 'utf8' |
deps | FileInAppDeps | 是 | 环境依赖 |
返回值
| 类型 | 说明 |
|---|---|
Promise<FileResult> | 成功含 data 与 errMsg: 'readFile:ok';缺路径或 IO 失败 reject |
writeFileInApp
函数签名
typescript
function writeFileInApp(options: FileOptions, deps: FileInAppDeps): Promise<FileResult>参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
options.filePath | string | 是 | 目标文件路径 |
options.data | string | ArrayBuffer | 是 | 写入内容 |
options.encoding | string | 否 | 编码,默认 'utf8' |
deps | FileInAppDeps | 是 | 环境依赖 |
返回值
| 类型 | 说明 |
|---|---|
Promise<FileResult> | 成功含 savedFilePath;缺 data/path 或写入失败 reject |
工作原理
- 读文件:通过
getFileSystemManager().readFile异步读取,成功映射为统一FileResult。 - 写文件:先对父目录调用
mkdir({ recursive: true });目录已存在时忽略file already exists错误后继续写入。 - 参数校验失败(无路径、无内容)在调用 FS API 前即 reject,错误信息为中文提示。