📌 从基础库 3.125.4 版本开始支持
获取文件 Stats 对象
属性 | 类型 | 默认值 | 必填 | 说明 | 最低版本 |
---|---|---|---|---|---|
path | string | 是 | 文件/目录路径 (本地路径) | ||
recursive | boolean | false | 否 | 是否递归获取目录下的每个文件的 Stats 信息 | 3.125.4 |
success | function | 否 | 接口调用成功的回调函数 | ||
fail | function | 否 | 接口调用失败的回调函数 | ||
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
属性 | 类型 | 说明 |
---|---|---|
stats | Stats/Array.< FileStats > | 当 recursive 为 false 时返回 Stats 对象;当 recursive 为 true 且 path 是目录路径时返回包含 path 和 stats 的对象数组 |
const fs = xhs.getFileSystemManager() // 异步版本 fs.stat({ path: `${xhs.env.USER_DATA_PATH}/testDir`, success: res => { console.log(res.stats.isDirectory()) } }) // 同步版本 try { const stats = fs.statSync(`${xhs.env.USER_DATA_PATH}/testDir`, false) console.log(stats.isDirectory()) } catch(e) { console.error(e) }
const fs = xhs.getFileSystemManager() // 异步版本 fs.stat({ path: `${xhs.env.USER_DATA_PATH}/testDir`, recursive: true, success: res => { res.stats.forEach(item => { console.log(item.path, item.stats.isDirectory()) }) } }) // 同步版本 try { const statsList = fs.statSync(`${xhs.env.USER_DATA_PATH}/testDir`, true) statsList.forEach(item => { console.log(item.path, item.stats.isDirectory()) }) } catch(e) { console.error(e) }