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
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
filePath | string | 是 | 要写入的文件路径 (本地路径) | |
data | string/ArrayBuffer | 是 | 要写入的文本或二进制数据 | |
encoding | string | utf8 | 否 | 指定写入文件的字符编码 |
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 | 接口调用失败的回调函数 | |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
object.encoding 的合法值
值 | 说明 | 最低版本 |
---|---|---|
ascii | ||
base64 | ||
binary | ||
hex | ||
ucs2 | 以小端序读取 | |
ucs-2 | 以小端序读取 | |
utf16le | 以小端序读取 | |
utf-16le | 以小端序读取 | |
utf-8 | ||
utf8 | ||
latin1 |
object.fail 回调函数
参数
Object res
属性 | 类型 | 说明 |
---|---|---|
errMsg | string | 错误信息 |
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 | 存储空间不足 |
标签:微信小程序
相关阅读 >>
更多相关阅读请进入《微信小程序》频道 >>

Vue.js 设计与实现 基于Vue.js 3 深入解析Vue.js 设计细节
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者