微信小程序API 文件-文件管理器


当前第2页 返回上一页

encoding 的合法值

说明 最低版本
ascii
base64
binary
hex
ucs2 以小端序读取
ucs-2 以小端序读取
utf16le 以小端序读取
utf-16le 以小端序读取
utf-8
utf8
latin1

string position

基础库 2.10.0 开始支持,低版本需做兼容处理。

从文件指定位置开始读,如果不指定,则从文件头开始读。读取的范围应该是左闭右开区间 [position, position+length)。有效范围:[0, fileLength - 1]。单位:byte

string length

基础库 2.10.0 开始支持,低版本需做兼容处理。

指定文件的长度,如果不指定,则读到文件末尾。有效范围:[1, fileLength]。单位:byte

返回值

string|ArrayBuffer data

文件内容

错误

错误码 错误信息 说明
fail no such file or directory, open ${filePath} 指定的 filePath 所在目录不存在
fail permission denied, open ${dirPath} 指定的 filePath 路径没有读权限


FileSystemManager.removeSavedFile(Object object)

删除该小程序下已保存的本地缓存文件

参数

Object object

属性 类型 默认值 必填 说明
filePath string 需要删除的文件路径 (本地路径)
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数(调用成功、失败都会执行)

object.fail 回调函数

参数
Object res
属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

说明 最低版本
fail file not exist 指定的 tempFilePath 找不到文件


FileSystemManager.rename(Object object)

重命名文件。可以把文件从 oldPath 移动到 newPath

参数

Object object

属性 类型 默认值 必填 说明
oldPath string 源文件路径,支持本地路径
newPath string 新文件路径,支持本地路径
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数(调用成功、失败都会执行)

object.fail 回调函数

参数
Object res
属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

说明 最低版本
fail permission denied, rename ${oldPath} -> ${newPath} 指定源文件或目标文件没有写权限
fail no such file or directory, rename ${oldPath} -> ${newPath} 源文件不存在,或目标文件路径的上层目录不存在


FileSystemManager.renameSync(string oldPath, string newPath)

FileSystemManager.rename 的同步版本

参数

string oldPath

源文件路径,支持本地路径

string newPath

新文件路径,支持本地路径

错误

错误码 错误信息 说明
fail permission denied, rename ${oldPath} -> ${newPath} 指定源文件或目标文件没有写权限
fail no such file or directory, rename ${oldPath} -> ${newPath} 源文件不存在,或目标文件路径的上层目录不存在


FileSystemManager.rmdir(Object object)

删除目录

参数

Object object

属性 类型 默认值 必填 说明 最低版本
dirPath string 要删除的目录路径 (本地路径)
recursive boolean false 是否递归删除目录。如果为 true,则删除该目录和该目录下的所有子目录以及文件。 2.3.0
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数(调用成功、失败都会执行)

object.fail 回调函数

参数
Object res
属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

说明 最低版本
fail no such file or directory ${dirPath} 目录不存在
fail directory not empty 目录不为空
fail permission denied, open ${dirPath} 指定的 dirPath 路径没有写权限


FileSystemManager.rmdirSync(string dirPath, boolean recursive)

FileSystemManager.rmdir 的同步版本

参数

string dirPath

要删除的目录路径 (本地路径)

boolean recursive

基础库 2.3.0 开始支持,低版本需做兼容处理。

是否递归删除目录。如果为 true,则删除该目录和该目录下的所有子目录以及文件。

错误

错误码 错误信息 说明
fail no such file or directory ${dirPath} 目录不存在
fail directory not empty 目录不为空
fail permission denied, open ${dirPath} 指定的 dirPath 路径没有写权限


FileSystemManager.saveFile(Object object)

保存临时文件到本地。此接口会移动临时文件,因此调用成功后,tempFilePath 将不可用。

参数

Object object

属性 类型 默认值 必填 说明
tempFilePath string 临时存储文件路径 (本地路径)
filePath string 要存储的文件路径 (本地路径)
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数(调用成功、失败都会执行)

object.success 回调函数

参数
Object res
属性 类型 说明
savedFilePath string 存储后的文件路径 (本地路径)

object.fail 回调函数

参数
Object res
属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

说明 最低版本
fail tempFilePath file not exist 指定的 tempFilePath 找不到文件
fail permission denied, open "${filePath}" 指定的 filePath 路径没有写权限
fail no such file or directory "${dirPath}" 上级目录不存在
fail the maximum size of the file storage limit is exceeded 存储空间不足


string FileSystemManager.saveFileSync(string tempFilePath, string filePath)

FileSystemManager.saveFile 的同步版本

参数

string tempFilePath

临时存储文件路径 (本地路径)

string filePath

要存储的文件路径 (本地路径)

返回值

string savedFilePath

存储后的文件路径 (本地路径)

错误

错误码 错误信息 说明
fail tempFilePath file not exist 指定的 tempFilePath 找不到文件
fail permission denied, open "${filePath}" 指定的 filePath 路径没有写权限
fail no such file or directory "${dirPath}" 上级目录不存在
fail the maximum size of the file storage limit is exceeded 存储空间不足


FileSystemManager.stat(Object object)

获取文件 Stats 对象

参数

Object object

属性 类型 默认值 必填 说明 最低版本
path string 文件/目录路径 (本地路径)
recursive boolean false 是否递归获取目录下的每个文件的 Stats 信息 2.3.0
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数(调用成功、失败都会执行)

object.success 回调函数

参数
Object res
属性 类型 说明
stats Stats/Object 当 recursive 为 false 时,res.stats 是一个 Stats 对象。当 recursive 为 true 且 path 是一个目录的路径时,res.stats 是一个 Object,key 以 path 为根路径的相对路径,value 是该路径对应的 Stats 对象。

object.fail 回调函数

参数
Object res
属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

说明 最低版本
fail permission denied, open ${path} 指定的 path 路径没有读权限
fail no such file or directory ${path} 文件不存在

示例代码

recursive 为 false 时


let fs = wx.getFileSystemManager()
fs.stat({
  path: `${wx.env.USER_DATA_PATH}/testDir`,
  success: res => {
    console.log(res.stats.isDirectory())
  }
})

recursive 为 true 时


fs.stat({
  path: `${wx.env.USER_DATA_PATH}/testDir`,
  recursive: true,
  success: res => {
    Object.keys(res.stats).forEach(path => {
      let stats = res.stats[path]
      console.log(path, stats.isDirectory())
    })
  }
})


Stats|Object FileSystemManager.statSync(string path, boolean recursive)

FileSystemManager.stat 的同步版本

参数

string path

文件/目录路径 (本地路径)

boolean recursive

基础库 2.3.0 开始支持,低版本需做兼容处理。

是否递归获取目录下的每个文件的 Stats 信息

返回值

Stats|Object stats

当 recursive 为 false 时,res.stats 是一个 Stats 对象。当 recursive 为 true 且 path 是一个目录的路径时,res.stats 是一个 Object,key 以 path 为根路径的相对路径,value 是该路径对应的 Stats 对象。

错误

错误码 错误信息 说明
fail permission denied, open ${path} 指定的 path 路径没有读权限
fail no such file or directory ${path} 文件不存在

示例代码

recursive 为 false 时


let fs = wx.getFileSystemManager()
fs.stat({
  path: `${wx.env.USER_DATA_PATH}/testDir`,
  success: res => {
    console.log(res.stats.isDirectory())
  }
})

recursive 为 true 时


fs.stat({
  path: `${wx.env.USER_DATA_PATH}/testDir`,
  recursive: true,
  success: res => {
    Object.keys(res.stats).forEach(path => {
      let stats = res.stats[path]
      console.log(path, stats.isDirectory())
    })
  }
})


FileSystemManager.unlink(Object object)

删除文件

参数

Object object

属性 类型 默认值 必填 说明
filePath string 要删除的文件路径 (本地路径)
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数(调用成功、失败都会执行)

object.fail 回调函数

参数
Object res
属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值


说明 最低版本
fail permission denied, open ${path} 指定的 path 路径没有读权限
fail no such file or directory ${path} 文件不存在
fail operation not permitted, unlink ${filePath} 传入的 filePath 是一个目录


FileSystemManager.unlinkSync(string filePath)

FileSystemManager.unlink 的同步版本

参数

string filePath

要删除的文件路径 (本地路径)

错误

错误码 错误信息 说明
fail permission denied, open ${path} 指定的 path 路径没有读权限
fail no such file or directory ${path} 文件不存在
fail operation not permitted, unlink ${filePath} 传入的 filePath 是一个目录


FileSystemManager.unzip(Object object)

解压文件

参数

Object object

属性 类型 默认值 必填 说明
zipFilePath string 源文件路径,支持本地路径, 只可以是 zip 压缩文件
targetPath string 目标目录路径, 支持本地路径
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数(调用成功、失败都会执行)

object.fail 回调函数

参数
Object res
属性 类型 说明
errMsg string 错误信息

res.errMsg 的合法值

说明 最低版本
fail permission denied, unzip ${zipFilePath} -> ${destPath} 指定目标文件路径没有写权限
fail no such file or directory, unzip ${zipFilePath} -> "${destPath} 源文件不存在,或目标文件路径的上层目录不存在


FileSystemManager.writeFile(Object object)

写文件

参数

Object object

属性类型默认值必填说明
filePathstring要写入的文件路径 (本地路径)
datastring/ArrayBuffer要写入的文本或二进制数据
encodingstringutf8指定写入文件的字符编码
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

object.encoding 的合法值

说明最低版本
ascii
base64
binary
hex
ucs2以小端序读取
ucs-2以小端序读取
utf16le以小端序读取
utf-16le以小端序读取
utf-8
utf8
latin1

object.fail 回调函数

参数
Object res
属性类型说明
errMsgstring错误信息

res.errMsg 的合法值

说明最低版本
fail no such file or directory, open ${filePath}指定的 filePath 所在目录不存在
fail permission denied, open ${dirPath}指定的 filePath 路径没有写权限
fail the maximum size of the file storage limit is exceeded存储空间不足


FileSystemManager.writeFileSync(string filePath, string|ArrayBuffer data, string encoding)

FileSystemManager.writeFile 的同步版本

参数

string filePath

要写入的文件路径 (本地路径)

string|ArrayBuffer data

要写入的文本或二进制数据

string encoding

指定写入文件的字符编码

encoding 的合法值

说明最低版本
ascii
base64
binary
hex
ucs2以小端序读取
ucs-2以小端序读取
utf16le以小端序读取
utf-16le以小端序读取
utf-8
utf8
latin1

错误

错误码错误信息说明
fail no such file or directory, open ${filePath}指定的 filePath 所在目录不存在
fail permission denied, open ${dirPath}指定的 filePath 路径没有写权限
fail the maximum size of the file storage limit is exceeded存储空间不足





标签:微信小程序

返回前面的内容

相关阅读 >>

微信小程序内容组件 text(文本)

sdk数据库 aggregate跳过指定数的文档

微信小程序api 背景音频-监听停止时间

微信小程序 图片高清化能力

微信小程序云开发api 指定查询排序条件

微信小程序云开发 兼容性问题

微信小程序 快递接口(商家查看)-相关接口和时间

微信小程序 getpaidunionid

微信小程序绘图api中创建二次方贝塞尔曲线

微信小程序 小程序使用precancelorder

更多相关阅读请进入《微信小程序》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...