Skip to main content
Version: 2.x

FileSystemManager

文件管理器

方法

readdirSync

FileSystemManager.readdir 的同步版本

参考文档

(dirPath: string) => string[]
参数类型说明
dirPathstring要读取的目录路径

API 支持度

API微信小程序H5React Native
FileSystemManager.readdirSync✔️

access

判断文件/目录是否存在

参考文档

(option: AccessOption) => void
参数类型
optionAccessOption

API 支持度

API微信小程序H5React Native
FileSystemManager.access✔️

accessSync

FileSystemManager.access 的同步版本

参考文档

(path: string) => void
参数类型说明
pathstring要判断是否存在的文件/目录路径

API 支持度

API微信小程序H5React Native
FileSystemManager.accessSync✔️

appendFile

在文件结尾追加内容

参考文档

(option: AppendFileOption) => void
参数类型
optionAppendFileOption

API 支持度

API微信小程序H5React Native
FileSystemManager.appendFile✔️

appendFileSync

FileSystemManager.appendFile 的同步版本

参考文档

(filePath: string, data: string | ArrayBuffer, encoding?: "ascii" | "base64" | "binary" | "hex" | "ucs2" | "ucs-2" | "utf16le" | "utf-16le" | "utf-8" | "utf8" | "latin1") => void
参数类型说明
filePathstring要追加内容的文件路径
datastring | ArrayBuffer要追加的文本或二进制数据
encoding"ascii" | "base64" | "binary" | "hex" | "ucs2" | "ucs-2" | "utf16le" | "utf-16le" | "utf-8" | "utf8" | "latin1"指定写入文件的字符编码

API 支持度

API微信小程序H5React Native
FileSystemManager.appendFileSync✔️

copyFile

复制文件

参考文档

(option: CopyFileOption) => void
参数类型
optionCopyFileOption

API 支持度

API微信小程序H5React Native
FileSystemManager.copyFile✔️

copyFileSync

FileSystemManager.copyFile 的同步版本

参考文档

(srcPath: string, destPath: string) => void
参数类型说明
srcPathstring源文件路径,只可以是普通文件
destPathstring目标文件路径

API 支持度

API微信小程序H5React Native
FileSystemManager.copyFileSync✔️

getFileInfo

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

参考文档

(option: getFileInfoOption) => void
参数类型
optiongetFileInfoOption

API 支持度

API微信小程序H5React Native
FileSystemManager.getFileInfo✔️

getSavedFileList

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

参考文档

(option?: getSavedFileListOption) => void
参数类型
optiongetSavedFileListOption

API 支持度

API微信小程序H5React Native
FileSystemManager.getSavedFileList✔️

mkdir

创建目录

参考文档

(option: MkdirOption) => void
参数类型
optionMkdirOption

API 支持度

API微信小程序H5React Native
FileSystemManager.mkdir✔️

mkdirSync

FileSystemManager.mkdir 的同步版本

参考文档

(dirPath: string, recursive?: boolean) => void
参数类型说明
dirPathstring创建的目录路径
recursiveboolean是否在递归创建该目录的上级目录后再创建该目录。如果对应的上级目录已经存在,则不创建该上级目录。如 dirPath 为 a/b/c/d 且 recursive 为 true,将创建 a 目录,再在 a 目录下创建 b 目录,以此类推直至创建 a/b/c 目录下的 d 目录。

API 支持度

API微信小程序H5React Native
FileSystemManager.mkdirSync✔️

readFile

读取本地文件内容

参考文档

(option: ReadFileOption) => void
参数类型
optionReadFileOption

API 支持度

API微信小程序H5React Native
FileSystemManager.readFile✔️

readdir

读取目录内文件列表

参考文档

(option: ReaddirOption) => void
参数类型
optionReaddirOption

API 支持度

API微信小程序H5React Native
FileSystemManager.readdir✔️

removeSavedFile

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

参考文档

(option: RemoveSavedFileOption) => void
参数类型
optionRemoveSavedFileOption

API 支持度

API微信小程序H5React Native
FileSystemManager.removeSavedFile✔️

rename

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

参考文档

(option: RenameOption) => void
参数类型
optionRenameOption

API 支持度

API微信小程序H5React Native
FileSystemManager.rename✔️

renameSync

FileSystemManager.rename 的同步版本

参考文档

(oldPath: string, newPath: string) => void
参数类型说明
oldPathstring源文件路径,可以是普通文件或目录
newPathstring新文件路径

API 支持度

API微信小程序H5React Native
FileSystemManager.renameSync✔️

rmdir

删除目录

参考文档

(option: RmdirOption) => void
参数类型
optionRmdirOption

API 支持度

API微信小程序H5React Native
FileSystemManager.rmdir✔️

rmdirSync

FileSystemManager.rmdir 的同步版本

参考文档

(dirPath: string, recursive?: boolean) => void
参数类型说明
dirPathstring要删除的目录路径
recursiveboolean是否递归删除目录。如果为 true,则删除该目录和该目录下的所有子目录以及文件。

API 支持度

API微信小程序H5React Native
FileSystemManager.rmdirSync✔️

saveFile

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

参考文档

(option: SaveFileOption) => void
参数类型
optionSaveFileOption

API 支持度

API微信小程序H5React Native
FileSystemManager.saveFile✔️

stat

获取文件 Stats 对象

(option: StatOption) => void
参数类型
optionStatOption

API 支持度

API微信小程序H5React Native
FileSystemManager.stat

删除文件

参考文档

(option: UnlinkOption) => void
参数类型
optionUnlinkOption

API 支持度

API微信小程序H5React Native
FileSystemManager.unlink✔️

unlinkSync

FileSystemManager.unlink 的同步版本

参考文档

(filePath: string) => void
参数类型说明
filePathstring要删除的文件路径

API 支持度

API微信小程序H5React Native
FileSystemManager.unlinkSync✔️

unzip

解压文件

参考文档

(option: UnzipOption) => void
参数类型
optionUnzipOption

API 支持度

API微信小程序H5React Native
FileSystemManager.unzip✔️

writeFile

写文件

参考文档

(option: WriteFileOption) => void
参数类型
optionWriteFileOption

API 支持度

API微信小程序H5React Native
FileSystemManager.writeFile✔️

writeFileSync

FileSystemManager.writeFile 的同步版本

参考文档

(filePath: string, data: string | ArrayBuffer, encoding?: "ascii" | "base64" | "binary" | "hex" | "ucs2" | "ucs-2" | "utf16le" | "utf-16le" | "utf-8" | "utf8" | "latin1") => void
参数类型说明
filePathstring要写入的文件路径
datastring | ArrayBuffer要写入的文本或二进制数据
encoding"ascii" | "base64" | "binary" | "hex" | "ucs2" | "ucs-2" | "utf16le" | "utf-16le" | "utf-8" | "utf8" | "latin1"指定写入文件的字符编码

API 支持度

API微信小程序H5React Native
FileSystemManager.writeFileSync✔️

statSync

FileSystemManager.stat 的同步版本

参考文档

(path: string, recursive?: boolean) => Stats | Record<string, any>
参数类型说明
pathstring文件/目录路径
recursiveboolean是否递归获取目录下的每个文件的 Stats 信息

API 支持度

API微信小程序H5React Native
FileSystemManager.statSync✔️

saveFileSync

FileSystemManager.saveFile 的同步版本

参考文档

(tempFilePath: string, filePath?: string) => number
参数类型说明
tempFilePathstring临时存储文件路径
filePathstring要存储的文件路径

API 支持度

API微信小程序H5React Native
FileSystemManager.saveFileSync✔️

readFileSync

FileSystemManager.readFile 的同步版本

参考文档

(filePath: string, encoding?: "ascii" | "base64" | "binary" | "hex" | "ucs2" | "ucs-2" | "utf16le" | "utf-16le" | "utf-8" | "utf8" | "latin1") => string | ArrayBuffer
参数类型说明
filePathstring要读取的文件的路径
encoding"ascii" | "base64" | "binary" | "hex" | "ucs2" | "ucs-2" | "utf16le" | "utf-16le" | "utf-8" | "utf8" | "latin1"指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容

API 支持度

API微信小程序H5React Native
FileSystemManager.readFileSync✔️

参数

encoding

字符编码

参数说明
ascii
base64
binary
hex
ucs2以小端序读取
ucs-2以小端序读取
utf16le以小端序读取
utf-16le以小端序读取
utf-8
utf8
latin1

AccessOption

参数类型必填说明
pathstring要判断是否存在的文件/目录路径
complete(res: CallbackResult) => void接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: AccessFailCallbackResult) => void接口调用失败的回调函数
success(res: CallbackResult) => void接口调用成功的回调函数

AccessFailCallbackResult

参数类型说明
errMsgstring错误信息

可选值:
- 'fail no such file or directory path': 文件/目录不存在;

AppendFileOption

参数类型必填说明
datastring | ArrayBuffer要追加的文本或二进制数据
filePathstring要追加内容的文件路径
complete(res: CallbackResult) => void接口调用结束的回调函数(调用成功、失败都会执行)
encoding"ascii" | "base64" | "binary" | "hex" | "ucs2" | "ucs-2" | "utf16le" | "utf-16le" | "utf-8" | "utf8" | "latin1"指定写入文件的字符编码
fail(result: AppendFileFailCallbackResult) => void接口调用失败的回调函数
success(res: CallbackResult) => void接口调用成功的回调函数

AppendFileFailCallbackResult

参数类型说明
errMsgstring错误信息

可选值:
- '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 是一个已经存在的目录;

CopyFileOption

参数类型必填说明
destPathstring目标文件路径
srcPathstring源文件路径,只可以是普通文件
complete(res: CallbackResult) => void接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: CopyFileFailCallbackResult) => void接口调用失败的回调函数
success(res: CallbackResult) => void接口调用成功的回调函数

CopyFileFailCallbackResult

参数类型说明
errMsgstring错误信息

可选值:
- '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': 存储空间不足;

getFileInfoOption

参数类型必填说明
filePathstring要读取的文件路径
complete(res: CallbackResult) => void接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: GetFileInfoFailCallbackResult) => void接口调用失败的回调函数
success(result: GetFileInfoSuccessCallbackResult) => void接口调用成功的回调函数

GetFileInfoFailCallbackResult

参数类型说明
errMsgstring错误信息

可选值:
- 'fail file not exist': 指定的 filePath 找不到文件;

GetFileInfoSuccessCallbackResult

参数类型说明
sizenumber文件大小,以字节为单位
errMsgstring调用结果

getSavedFileListOption

参数类型必填说明
complete(res: CallbackResult) => void接口调用结束的回调函数(调用成功、失败都会执行)
fail(res: CallbackResult) => void接口调用失败的回调函数
success(result: GetSavedFileListSuccessCallbackResult) => void接口调用成功的回调函数

GetSavedFileListSuccessCallbackResult

参数类型说明
fileListGetSavedFileListSuccessCallbackResultFileItem[]文件数组
errMsgstring调用结果

GetSavedFileListSuccessCallbackResultFileItem

文件数组

参数类型说明
createTimenumber文件保存时的时间戳,从1970/01/01 08:00:00 到当前时间的秒数
filePathstring本地路径
sizenumber本地文件大小,以字节为单位

MkdirOption

参数类型必填说明
dirPathstring创建的目录路径
complete(res: CallbackResult) => void接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: MkdirFailCallbackResult) => void接口调用失败的回调函数
recursiveboolean是否在递归创建该目录的上级目录后再创建该目录。如果对应的上级目录已经存在,则不创建该上级目录。如 dirPath 为 a/b/c/d 且 recursive 为 true,将创建 a 目录,再在 a 目录下创建 b 目录,以此类推直至创建 a/b/c 目录下的 d 目录。
success(res: CallbackResult) => void接口调用成功的回调函数

MkdirFailCallbackResult

参数类型说明
errMsgstring错误信息

可选值:
- 'fail no such file or directory dirPath': 上级目录不存在;
- 'fail permission denied, open dirPath': 指定的 filePath 路径没有写权限;
- 'fail file already exists dirPath': 有同名文件或目录;

ReadFileOption

参数类型必填说明
filePathstring要读取的文件的路径
complete(res: CallbackResult) => void接口调用结束的回调函数(调用成功、失败都会执行)
encoding"ascii" | "base64" | "binary" | "hex" | "ucs2" | "ucs-2" | "utf16le" | "utf-16le" | "utf-8" | "utf8" | "latin1"指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容
fail(result: ReadFileFailCallbackResult) => void接口调用失败的回调函数
success(res: CallbackResult) => void接口调用成功的回调函数

ReadFileFailCallbackResult

参数类型说明
errMsgstring错误信息

可选值:
- 'fail no such file or directory, open filePath': 指定的 filePath 所在目录不存在;
- 'fail permission denied, open dirPath': 指定的 filePath 路径没有读权限;

ReaddirOption

参数类型必填说明
dirPathstring要读取的目录路径
complete(res: CallbackResult) => void接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: ReaddirFailCallbackResult) => void接口调用失败的回调函数
success(result: ReaddirSuccessCallbackResult) => void接口调用成功的回调函数

ReaddirFailCallbackResult

参数类型说明
errMsgstring错误信息

可选值:
- 'fail no such file or directory dirPath': 目录不存在;
- 'fail not a directory dirPath': dirPath 不是目录;
- 'fail permission denied, open dirPath': 指定的 filePath 路径没有读权限;

ReaddirSuccessCallbackResult

参数类型说明
filesstring[]指定目录下的文件名数组。
errMsgstring调用结果

RemoveSavedFileOption

参数类型必填说明
filePathstring需要删除的文件路径
complete(res: CallbackResult) => void接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: RemoveSavedFileFailCallbackResult) => void接口调用失败的回调函数
success(res: CallbackResult) => void接口调用成功的回调函数

RemoveSavedFileFailCallbackResult

参数类型说明
errMsgstring错误信息

可选值:
- 'fail file not exist': 指定的 tempFilePath 找不到文件;

RenameOption

参数类型必填说明
newPathstring新文件路径
oldPathstring源文件路径,可以是普通文件或目录
complete(res: CallbackResult) => void接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: RenameFailCallbackResult) => void接口调用失败的回调函数
success(res: CallbackResult) => void接口调用成功的回调函数

RenameFailCallbackResult

参数类型说明
errMsgstring错误信息

可选值:
- 'fail permission denied, rename oldPath -> newPath': 指定源文件或目标文件没有写权限;
- 'fail no such file or directory, rename oldPath -> newPath': 源文件不存在,或目标文件路径的上层目录不存在;

RmdirOption

参数类型必填说明
dirPathstring要删除的目录路径
complete(res: CallbackResult) => void接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: RmdirFailCallbackResult) => void接口调用失败的回调函数
recursiveboolean是否递归删除目录。如果为 true,则删除该目录和该目录下的所有子目录以及文件。
success(res: CallbackResult) => void接口调用成功的回调函数

RmdirFailCallbackResult

参数类型说明
errMsgstring错误信息

可选值:
- 'fail no such file or directory dirPath': 目录不存在;
- 'fail directory not empty': 目录不为空;
- 'fail permission denied, open dirPath': 指定的 dirPath 路径没有写权限;

SaveFileOption

参数类型必填说明
tempFilePathstring临时存储文件路径
complete(res: CallbackResult) => void接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: SaveFileFailCallbackResult) => void接口调用失败的回调函数
filePathstring要存储的文件路径
success(result: SaveFileSuccessCallbackResult) => void接口调用成功的回调函数

SaveFileFailCallbackResult

参数类型说明
errMsgstring错误信息

可选值:
- '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': 存储空间不足;

SaveFileSuccessCallbackResult

参数类型说明
savedFilePathnumber存储后的文件路径
errMsgstring调用结果

StatOption

参数类型必填说明
pathstring文件/目录路径
complete(res: CallbackResult) => void接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: StatFailCallbackResult) => void接口调用失败的回调函数
recursiveboolean是否递归获取目录下的每个文件的 Stats 信息
success(result: StatSuccessCallbackResult) => void接口调用成功的回调函数

StatFailCallbackResult

参数类型说明
errMsgstring错误信息

可选值:
- 'fail permission denied, open path': 指定的 path 路径没有读权限;
- 'fail no such file or directory path': 文件不存在;

StatSuccessCallbackResult

参数类型说明
statsStats | Record<string, any>Stats
errMsgstring调用结果

UnlinkOption

参数类型必填说明
filePathstring要删除的文件路径
complete(res: CallbackResult) => void接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: UnlinkFailCallbackResult) => void接口调用失败的回调函数
success(res: CallbackResult) => void接口调用成功的回调函数

UnlinkFailCallbackResult

参数类型说明
errMsgstring错误信息

可选值:
- 'fail permission denied, open path': 指定的 path 路径没有读权限;
- 'fail no such file or directory path': 文件不存在;
- 'fail operation not permitted, unlink filePath': 传入的 filePath 是一个目录;

UnzipOption

参数类型必填说明
targetPathstring目标目录路径
zipFilePathstring源文件路径,只可以是 zip 压缩文件
complete(res: CallbackResult) => void接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: UnzipFailCallbackResult) => void接口调用失败的回调函数
success(res: CallbackResult) => void接口调用成功的回调函数

UnzipFailCallbackResult

参数类型说明
errMsgstring错误信息

可选值:
- 'fail permission denied, unzip zipFilePath -> destPath': 指定目标文件路径没有写权限;
- 'fail no such file or directory, unzip zipFilePath -> "destPath': 源文件不存在,或目标文件路径的上层目录不存在;

WriteFileOption

参数类型必填说明
datastring | ArrayBuffer要写入的文本或二进制数据
filePathstring要写入的文件路径
complete(res: CallbackResult) => void接口调用结束的回调函数(调用成功、失败都会执行)
encoding"ascii" | "base64" | "binary" | "hex" | "ucs2" | "ucs-2" | "utf16le" | "utf-16le" | "utf-8" | "utf8" | "latin1"指定写入文件的字符编码
fail(result: WriteFileFailCallbackResult) => void接口调用失败的回调函数
success(res: CallbackResult) => void接口调用成功的回调函数

WriteFileFailCallbackResult

参数类型说明
errMsgstring错误信息

可选值:
- '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': 存储空间不足;

API 支持度

API微信小程序H5React Native
FileSystemManager.readdirSync✔️
FileSystemManager.access✔️
FileSystemManager.accessSync✔️
FileSystemManager.appendFile✔️
FileSystemManager.appendFileSync✔️
FileSystemManager.copyFile✔️
FileSystemManager.copyFileSync✔️
FileSystemManager.getFileInfo✔️
FileSystemManager.getSavedFileList✔️
FileSystemManager.mkdir✔️
FileSystemManager.mkdirSync✔️
FileSystemManager.readFile✔️
FileSystemManager.readdir✔️
FileSystemManager.removeSavedFile✔️
FileSystemManager.rename✔️
FileSystemManager.renameSync✔️
FileSystemManager.rmdir✔️
FileSystemManager.rmdirSync✔️
FileSystemManager.saveFile✔️
FileSystemManager.stat
FileSystemManager.unlink✔️
FileSystemManager.unlinkSync✔️
FileSystemManager.unzip✔️
FileSystemManager.writeFile✔️
FileSystemManager.writeFileSync✔️
FileSystemManager.statSync✔️
FileSystemManager.saveFileSync✔️
FileSystemManager.readFileSync✔️