FileSystemManager.appendFile
收藏我的收藏
基础库 2.60.0 开始支持本方法,低版本需做兼容处理,这是一个异步方法。
在文件结尾追加内容,只有用户目录(ttfile://user)下的文件可以追加,文件不存在时会新建。
| 前提条件 | 无 |
| 业务背景 | 无 |
| 使用限制 | 无 |
| 注意事项 | 无 |
| 支持沙盒 | 否 |
| 相关教程 | 无 |
语法
FileSystemManager.appendFile(options)
参数说明
options 为 object 类型,属性如下:
| 属性名 | 类型 | 默认值 | 必填 | 说明 | 最低支持版本 |
|---|---|---|---|---|---|
| encoding | enum | utf-8 | 否 | 指定写入文件的字符编码 | 2.60.0 |
| data | string | arraybuffer | 是 | 要写入的文本或二进制数据 | 2.60.0 | |
| filePath | string | 是 | 要追加内容的文件路径,必须以 `ttfile://user` 开头 | 2.60.0 | |
| success | function | 否 | 接口调用成功的回调函数 | 2.60.0 | |
| fail | function | 否 | 接口调用失败的回调函数 | 2.60.0 | |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | 2.60.0 |
encoding 的合法值
| 值 | 说明 | 最低支持版本 |
|---|---|---|
| utf-8 | 2.60.0 | |
| ascii | 2.60.0 | |
| base64 | 2.60.0 | |
| binary | ISO-8859-1 | 2.60.0 |
| hex | 十六进制 | 2.60.0 |
| ucs2 | 以小端序读取 | 2.60.0 |
| ucs-2 | 以小端序读取 | 2.60.0 |
| utf16le | 以小端序读取 | 2.60.0 |
| utf-16le | 以小端序读取 | 2.60.0 |
| utf8 | 2.60.0 | |
| latin1 | ISO-8859-1 | 2.60.0 |
回调成功
object 类型,属性如下:
| 属性名 | 类型 | 说明 | 最低支持版本 |
|---|---|---|---|
| errMsg | string | "appendFile:ok" | 2.60.0 |
回调失败
object 类型,属性如下:
| 属性名 | 类型 | 说明 | 最低支持版本 |
|---|---|---|---|
| errMsg | string | "appendFile:fail " + 详细错误信息 | 2.60.0 |
错误码
| errorCode | errMsg | errorType | 说明 | 最低支持版本 |
|---|---|---|---|---|
| 108001 | permission denied, %s %s | D | 路径没有权限 请查看filePath参数是否正确 | 2.60.0 |
| 108002 | no such file or directory, %s%s | D | 文件不存在 请查看filePath参数是否正确 | 2.60.0 |
| 108004 | operation not permitted, %s %s | D | 文件路径读取后应该是一个文件 请查看filePath参数是否正确 | 2.60.0 |
| 108091 | sandbox is nil | F | 小程序框架内部错误,有需要请创建工单咨询 | 2.60.0 |
| 108091 | operation fail | F | 小程序框架内部错误,有需要请创建工单咨询 | 2.60.0 |
| 161799 | params filePath is required | D | 参数filePath为空 请确保filePath参数不为空 | 2.60.0 |
扫码体验
请使用字节宿主APP扫码
代码示例
const fs = tt.getFileSystemManager() fs.appendFile({ filePath: `ttfile://user/example.txt`, data: "Hello World", encoding: "utf8", success(res) { console.log(res) }, fail(res) { console.error(res) }, }) // 同步接口 try { fs.appendFileSync(`ttfile://user/example.txt`, "Hello World", "utf8") } catch (e) { console.error(e) }
