FileSystemManager.getFileInfo
收藏
我的收藏

基础库 1.15.0 开始支持本方法,这是一个异步方法。

获取 临时目录(ttfile://temp) 或 用户目录(ttfile://user) 下的文件信息。

前提条件
业务背景
使用限制
注意事项
支持沙盒
相关教程

语法

FileSystemManager.getFileInfo(options)

参数说明

options 为 object 类型,属性如下:

属性名类型默认值必填说明最低支持版本
digestAlgorithmenummd5
计算文件摘要的算法
1.81.0
filePathstring
要读取的文件路径
1.15.0
successfunction
接口调用成功的回调函数
1.15.0
failfunction
接口调用失败的回调函数
1.15.0
completefunction
接口调用结束的回调函数(调用成功、失败都会执行)
1.15.0

digestAlgorithm 的合法值

说明最低支持版本
sha1
sha1 算法
1.81.0
md5
md5 算法
1.81.0

回调成功

object 类型,属性如下:

属性名类型说明最低支持版本
sizenumber

文件大小,以字节为单位

1.15.0
digeststring

按照传入的 digestAlgorithm 计算得出的的文件摘要

1.81.0
errMsgstring

"getFileInfo:ok"

1.15.0

回调失败

object 类型,属性如下:

属性名类型说明最低支持版本
errMsgstring

"getFileInfo:fail " + 错误信息

1.15.0

错误码

errNoerrMsg说明最低支持版本
20000getFileInfo:fail filePath is invalid
filePath 参数错误
1.99.0
21101getFileInfo:fail permission denied, getFileInfo %s
文件路径不可读
1.99.0
21102getFileInfo:fail no such file or directory, getFileInfo %s
文件不存在
1.99.0
21103getFileInfo:fail operation not permitted, getFileInfo %s
filePath 类型不是文件
1.99.0

扫码体验

请使用字节宿主APP扫码

代码示例

开发者工具中预览

const fileSystemManager = tt.getFileSystemManager()

tt.chooseImage({
  success(res) {
    getFileInfo(res.tempFilePaths[0])
  },
})

function getFileInfo(filePath) {
  fileSystemManager.getFileInfo({
    filePath,
    digestAlgorithm: "md5",
    success(res) {
      console.log("文件信息:", res)
    },
    fail(res) {
      console.log("调用失败", res.errMsg)
    },
  })
}