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


本文整理自网络,侵删。

FileSystemManager

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

文件管理器


方法:

FileSystemManager.access(Object object)

判断文件/目录是否存在

参数

Object object

属性 类型 默认值 必填 说明
path string 要判断是否存在的文件/目录路径 (本地路径)
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数(调用成功、失败都会执行)

object.fail 回调函数

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

res.errMsg 的合法值

说明 最低版本
fail no such file or directory ${path} 文件/目录不存在



FileSystemManager.accessSync(string path)

FileSystemManager.access 的同步版本

参数

string path

要判断是否存在的文件/目录路径 (本地路径)

错误

错误码 错误信息 说明
fail no such file or directory ${path} 文件/目录不存在


FileSystemManager.appendFile(Object object)

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

在文件结尾追加内容

参数

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 illegal operation on a directory, open "${filePath}" 指定的 filePath 是一个已经存在的目录
fail permission denied, open ${dirPath} 指定的 filePath 路径没有写权限
fail sdcard not mounted 指定的 filePath 是一个已经存在的目录


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

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

FileSystemManager.appendFile 的同步版本

参数

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 illegal operation on a directory, open "${filePath}" 指定的 filePath 是一个已经存在的目录
fail permission denied, open ${dirPath} 指定的 filePath 路径没有写权限
fail sdcard not mounted 指定的 filePath 是一个已经存在的目录


FileSystemManager.copyFile(Object object)

复制文件

参数

Object object

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

object.fail 回调函数

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

res.errMsg 的合法值

说明 最低版本
fail permission denied, copyFile ${srcPath} -> ${destPath} 指定目标文件路径没有写权限
fail no such file or directory, copyFile ${srcPath} -> ${destPath} 源文件不存在,或目标文件路径的上层目录不存在
fail the maximum size of the file storage limit is exceeded 存储空间不足


FileSystemManager.copyFileSync(string srcPath, string destPath)

FileSystemManager.copyFile 的同步版本

参数

string srcPath

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

string destPath

目标文件路径,支持本地路径

错误

错误码 错误信息 说明
fail permission denied, copyFile ${srcPath} -> ${destPath} 指定目标文件路径没有写权限
fail no such file or directory, copyFile ${srcPath} -> ${destPath} 源文件不存在,或目标文件路径的上层目录不存在
fail the maximum size of the file storage limit is exceeded 存储空间不足


FileSystemManager.getFileInfo(Object object)

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

参数

Object object

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

object.success 回调函数

参数
Object res
属性 类型 说明
size number 文件大小,以字节为单位

object.fail 回调函数

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

res.errMsg 的合法值

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


FileSystemManager.getSavedFileList(Object object)

获取该小程序下已保存的本地缓存文件列表

参数

Object object

属性 类型 默认值 必填 说明
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数(调用成功、失败都会执行)

object.success 回调函数

参数
Object res
属性 类型 说明
fileList Array.<Object> 文件数组

res.fileList 的结构

属性 类型 说明
filePath string 文件路径 (本地路径)
size number 本地文件大小,以字节为单位
createTime number 文件保存时的时间戳,从1970/01/01 08:00:00 到当前时间的秒数


FileSystemManager.mkdir(Object object)

创建目录

参数

Object object

属性 类型 默认值 必填 说明 最低版本
dirPath string 创建的目录路径 (本地路径)
recursive boolean false 是否在递归创建该目录的上级目录后再创建该目录。如果对应的上级目录已经存在,则不创建该上级目录。如 dirPath 为 a/b/c/d 且 recursive 为 true,将创建 a 目录,再在 a 目录下创建 b 目录,以此类推直至创建 a/b/c 目录下的 d 目录。 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 permission denied, open ${dirPath} 指定的 filePath 路径没有写权限
fail file already exists ${dirPath} 有同名文件或目录


FileSystemManager.mkdirSync(string dirPath, boolean recursive)

FileSystemManager.mkdir 的同步版本

参数

string dirPath

创建的目录路径 (本地路径)

boolean recursive

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

是否在递归创建该目录的上级目录后再创建该目录。如果对应的上级目录已经存在,则不创建该上级目录。如 dirPath 为 a/b/c/d 且 recursive 为 true,将创建 a 目录,再在 a 目录下创建 b 目录,以此类推直至创建 a/b/c 目录下的 d 目录。

错误

错误码 错误信息 说明
fail no such file or directory ${dirPath} 上级目录不存在
fail permission denied, open ${dirPath} 指定的 filePath 路径没有写权限
fail file already exists ${dirPath} 有同名文件或目录


FileSystemManager.readdir(Object object)

读取目录内文件列表

参数

Object object

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

object.success 回调函数

参数
Object res
属性 类型 说明
files Array.<string> 指定目录下的文件名数组。

object.fail 回调函数

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

res.errMsg 的合法值

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


Array.<string> FileSystemManager.readdirSync(string dirPath)

FileSystemManager.readdir 的同步版本

参数

string dirPath

要读取的目录路径 (本地路径)

返回值

Array.<string> files

指定目录下的文件名数组。

错误

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


FileSystemManager.readFile(Object object)

读取本地文件内容

参数

Object object

属性 类型 默认值 必填 说明 最低版本
filePath string 要读取的文件的路径 (本地路径)
encoding string 指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容
position string 从文件指定位置开始读,如果不指定,则从文件头开始读。读取的范围应该是左闭右开区间 [position, position+length)。有效范围:[0, fileLength - 1]。单位:byte 2.10.0
length string 指定文件的长度,如果不指定,则读到文件末尾。有效范围:[1, fileLength]。单位:byte 2.10.0
success function 接口调用成功的回调函数
fail function 接口调用失败的回调函数
complete function 接口调用结束的回调函数(调用成功、失败都会执行)

object.encoding 的合法值

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

object.success 回调函数

参数
Object res
属性 类型 说明
data string/ArrayBuffer 文件内容

object.fail 回调函数

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

res.errMsg 的合法值

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


string|ArrayBuffer FileSystemManager.readFileSync(string filePath, string encoding, string position, string length)

FileSystemManager.readFile 的同步版本

参数

string filePath

要读取的文件的路径 (本地路径)

string encoding

指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容

阅读剩余部分

相关阅读 >>

微信小程序 darkmode适配指南

微信小程序 getplugindevapplylist

微信小程序 启动性能

微信小程序 扩展组件纵向选项卡组件

微信小程序视图容器 cover-image

微信小程序 访问留存

微信小程序云开发sdk文档 工具类

微信小程序工具 快捷键

微信小程序 ocrbankcard

微信小程序 快递接口(商家查看-常见问题

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




打赏

取消

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

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

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

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

评论

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