FileSystemManager.readFile
基础库 1.15.0 开始支持本方法,低版本需做兼容处理。
读取本地文件内容。
语法
FileSystemManager.readFile(options)
参数说明
options
options 为 object 类型,属性描述如下:
属性名 | 类型 | 默认值 | 必填 | 说明 | 最低支持版本 |
---|---|---|---|---|---|
filePath | string | 是 | 要读取的文件的路径 | 1.15.0 | |
encoding | string | 否 | 指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容 | 1.15.0 | |
success | Function | 否 | 接口调用成功的回调函数 | 1.15.0 | |
fail | Function | 否 | 接口调用失败的回调函数 | 1.15.0 | |
complete | Function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) | 1.15.0 |
encoding 的合法值
值 | 说明 |
---|---|
ascii | |
base64 | |
binary | |
hex | |
ucs2/ucs-2/utf16le/utf-16le | 以小端序读取 |
utf-8/utf8 | |
latin1 |
回调成功
回调参数为 object 类型,属性如下:
属性 | 类型 | 说明 | 最低支持版本 |
---|---|---|---|
data | string | ArrayBuffer | 数据 | 1.15.0 |
回调失败
回调参数为 object 类型,属性如下:
属性 | 类型 | 说明 | 最低支持版本 |
---|---|---|---|
errMsg | string | "readFile:fail " + 详细错误信息 | 1.15.0 |
errMsg 说明 | 说明 |
---|---|
fail no such file or directory, open ${filePath} | 指定的 filePath 所在目录不存在 |
fail permission denied, open ${dirPath} | 指定的 filePath 路径没有读权限 |
扫码体验
请使用字节宿主APP扫码
代码示例
const fileSystemManager = tt.getFileSystemManager();
tt.chooseImage({
success(res) {
readFile(res.tempFilePaths[0]);
},
});
function readFile(filePath) {
fileSystemManager.readFile({
filePath,
success(res) {
// 输出读取的文件内容
console.log(res.data);
},
fail(res) {
console.log("调用失败", res.errMsg);
},
});
}
Bug & Tip
- Tip:
readFile
可以用于 用户目录(ttfile://user
) 临时目录(ttfile://temp
) 以及 包内目录(访问包内目录不需要加额外协议头)
点击纠错
评价此篇文档