小程序开放平台

文档中心
基础
路由
跳转
转发
界面
网络
支付
数据缓存
媒体
位置
开放接口
设备
XHSML
性能
第三方平台
文件
removeSavedFile
openDocument
getFileSystemManager
FileSystemManager方法
access
accessSync
appendFile
appendFileSync
copyFile
copyFileSync
getSavedFileList
getFileInfo
mkdir
mkdirSync
readdir
readdirSync
rmdir
rmdirSync
rename
renameSync
readFile
readFileSync
removeSavedFile
stat
statSync
saveFile
saveFileSync
truncate
truncateSync
unlink
unlinkSync
unzip
writeFile
writeFileSync
Stats
FileStats
错误码
数据分析
画布

fs.getFileInfo

开发
>
JS API
>
文件
>
FileSystemManager方法
>
getFileInfo
>
更新时间:2025-07-23 11:40:59

📌 从基础库3.125.4版本开始支持

功能描述

获取该小程序下的本地临时文件或本地缓存文件信息

参数

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

digestAlgorithm 的合法值

说明
md5md5 算法
sha1sha1 算法
sha256sha256 算法

返回值

success 回调参数

属性
类型
说明
sizenumber文件大小,以字节为单位
digeststring按照传入的 digestAlgorithm 计算得出的的文件摘要

示例代码

javascript
const fs = xhs.getFileSystemManager()

// 异步调用
fs.getFileInfo({
  filePath: `${xhs.env.USER_DATA_PATH}/hello.txt`,
  digestAlgorithm: 'md5',
  success(res) {
    console.log('文件大小:', res.size)
    console.log('文件摘要:', res.digest)
  },
  fail(res) {
    console.error(res)
  }
})