跳到主要内容
版本:4.x

FileSystemManager

文件管理器,可通过 Taro.getFileSystemManager 获取。

支持情况:

参考文档

方法​

access​

判断文件/目录是否存在

支持情况:

参考文档

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

accessSync​

FileSystemManager.access 的同步版本

支持情况:

参考文档

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

appendFile​

在文件结尾追加内容

支持情况:

参考文档

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

appendFileSync​

FileSystemManager.appendFile 的同步版本

支持情况:

参考文档

(filePath: string, data: string | ArrayBuffer, encoding?: keyof Encoding) => void
参数类型说明
filePathstring要追加内容的文件路径
datastring or ArrayBuffer要追加的文本或二进制数据
encodingkeyof Encoding指定写入文件的字符编码

close​

关闭文件

支持情况:

参考文档

(option: CloseOption) => void
参数类型
optionCloseOption

closeSync​

FileSystemManager.close 的同步版本

支持情况:

参考文档

(option: CloseSyncOption) => void
参数类型
optionCloseSyncOption

copyFile​

复制文件

支持情况:

参考文档

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

copyFileSync​

FileSystemManager.copyFile 的同步版本

支持情况:

参考文档

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

fstat​

获取文件的状态信息

支持情况:

参考文档

(option: FstatOption) => void
参数类型
optionFstatOption

fstatSync​

FileSystemManager.fstat 的同步版本

支持情况:

参考文档

(option: FstatSyncOption) => Stats
参数类型
optionFstatSyncOption

ftruncate​

对文件内容进行截断操作

支持情况:

参考文档

(option: FtruncateOption) => void
参数类型
optionFtruncateOption

ftruncateSync​

FileSystemManager.ftruncate 的同步版本

支持情况:

参考文档

(option: FtruncateSyncOption) => void
参数类型
optionFtruncateSyncOption

getFileInfo​

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

支持情况:

参考文档

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

getSavedFileList​

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

支持情况:

参考文档

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

mkdir​

创建目录

支持情况:

参考文档

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

mkdirSync​

FileSystemManager.mkdir 的同步版本

支持情况:

参考文档

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

open​

打开文件,返回文件描述符

支持情况:

参考文档

(option: OpenOption) => void
参数类型
optionOpenOption

openSync​

FileSystemManager.openSync 的同步版本

支持情况:

参考文档

(option: OpenSyncOption) => string
参数类型
optionOpenSyncOption

read​

读文件

支持情况:

参考文档

(option: ReadOption) => void
参数类型
optionReadOption

readCompressedFile​

读取指定压缩类型的本地文件内容

支持情况:

参考文档

(option: Option) => Promise<Promised>
参数类型
optionOption

readCompressedFileSync​

同步读取指定压缩类型的本地文件内容

支持情况:

参考文档

(option: Option) => ArrayBuffer
参数类型
optionOption

readdir​

读取目录内文件列表

支持情况:

参考文档

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

readdirSync​

FileSystemManager.readdir 的同步版本

支持情况:

参考文档

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

readFile​

读取本地文件内容

支持情况:

参考文档

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

readFileSync​

FileSystemManager.readFile 的同步版本

支持情况:

参考文档

(filePath: string, encoding?: keyof Encoding, position?: number, length?: number) => string | ArrayBuffer
参数类型说明
filePathstring要读取的文件的路径
encodingkeyof Encoding指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容
positionnumber从文件指定位置开始读,如果不指定,则从文件头开始读。读取的范围应该是左闭右开区间 [position, position+length)。有效范围:[0, fileLength - 1]。单位:byte
lengthnumber指定文件的长度,如果不指定,则读到文件末尾。有效范围:[1, fileLength]。单位:byte

readSync​

FileSystemManager.read 的同步版本

支持情况:

参考文档

(option: ReadSyncOption) => { bytesRead: number; arrayBuffer: ArrayBuffer; }
参数类型
optionReadSyncOption

readZipEntry​

读取压缩包内的文件

支持情况:

参考文档

(option: Option) => Promise<Promised>
参数类型
optionOption

removeSavedFile​

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

支持情况:

参考文档

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

rename​

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

支持情况:

参考文档

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

renameSync​

FileSystemManager.rename 的同步版本

支持情况:

参考文档

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

rmdir​

删除目录

支持情况:

参考文档

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

rmdirSync​

FileSystemManager.rmdir 的同步版本

支持情况:

参考文档

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

saveFile​

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

支持情况:

参考文档

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

saveFileSync​

FileSystemManager.saveFile 的同步版本

支持情况:

参考文档

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

stat​

获取文件 Stats 对象

支持情况:

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

statSync​

FileSystemManager.stat 的同步版本

支持情况:

参考文档

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

truncate​

对文件内容进行截断操作

支持情况:

参考文档

(option: TruncateOption) => void
参数类型
optionTruncateOption

truncateSync​

对文件内容进行截断操作 (truncate 的同步版本)

支持情况:

参考文档

(option: TruncateSyncOption) => void
参数类型
optionTruncateSyncOption

删除文件

支持情况:

参考文档

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

unlinkSync​

FileSystemManager.unlink 的同步版本

支持情况:

参考文档

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

unzip​

解压文件

支持情况:

参考文档

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

write​

写入文件

支持情况:

参考文档

(option: WriteOption) => void
参数类型
optionWriteOption

writeFile​

写文件

支持情况:

参考文档

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

writeFileSync​

FileSystemManager.writeFile 的同步版本

支持情况:

参考文档

(filePath: string, data: string | ArrayBuffer, encoding?: keyof Encoding) => void
参数类型说明
filePathstring要写入的文件路径
datastring or ArrayBuffer要写入的文本或二进制数据
encodingkeyof Encoding指定写入文件的字符编码

writeSync​

write 的同步版本

支持情况:

参考文档

(option: WriteSyncOption) => { bytesWritten: number; }
参数类型
optionWriteSyncOption

参数​

Encoding​

字符编码

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

flag​

文件系统标志

AccessOption​

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

AccessFailCallbackResult​

参数类型说明
errMsgstring错误信息

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

AppendFileOption​

参数类型必填说明
datastring or ArrayBuffer是要追加的文本或二进制数据
filePathstring是要追加内容的文件路径
encodingkeyof Encoding否指定写入文件的字符编码
complete(res: TaroGeneral.CallbackResult) => void否接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: AppendFileFailCallbackResult) => void否接口调用失败的回调函数
success(res: TaroGeneral.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: TaroGeneral.CallbackResult) => void否接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: CopyFileFailCallbackResult) => void否接口调用失败的回调函数
success(res: TaroGeneral.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是要读取的文件路径
digestAlgorithm"md5" or "sha1"'md5'否计算文件摘要的算法
complete(res: TaroGeneral.CallbackResult) => void否接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: GetFileInfoFailCallbackResult) => void否接口调用失败的回调函数
success(result: GetFileInfoSuccessCallbackResult) => void否接口调用成功的回调函数

GetFileInfoFailCallbackResult​

参数类型说明
errMsgstring错误信息

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

GetFileInfoSuccessCallbackResult​

参数类型必填说明
sizenumber是文件大小,以字节为单位
digeststring否按照传入的 digestAlgorithm 计算得出的的文件摘要
errMsgstring是调用结果

getSavedFileListOption​

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

GetSavedFileListSuccessCallbackResult​

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

GetSavedFileListSuccessCallbackResultFileItem​

文件数组

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

MkdirOption​

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

MkdirFailCallbackResult​

参数类型说明
errMsgstring错误信息

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

ReadFileOption​

参数类型必填说明
filePathstring是要读取的文件的路径
positionnumber否从文件指定位置开始读,如果不指定,则从文件头开始读。读取的范围应该是左闭右开区间 [position, position+length)。有效范围:[0, fileLength - 1]。单位:byte
lengthnumber否指定文件的长度,如果不指定,则读到文件末尾。有效范围:[1, fileLength]。单位:byte
complete(res: TaroGeneral.CallbackResult) => void否接口调用结束的回调函数(调用成功、失败都会执行)
encodingkeyof Encoding否指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容
fail(result: ReadFileFailCallbackResult) => void否接口调用失败的回调函数
success(res: ReadFileSuccessCallbackResult) => void否接口调用成功的回调函数

ReadFileSuccessCallbackResult​

参数类型说明
datastring or ArrayBuffer文件内容
errMsgstring调用结果

ReadFileFailCallbackResult​

参数类型说明
errMsgstring错误信息

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

ReaddirOption​

参数类型必填说明
dirPathstring是要读取的目录路径
complete(res: TaroGeneral.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调用结果

readZipEntry​

Promised​

FailCallbackResult | SuccessCallbackResult

Option​

参数类型必填说明
filePathstring是要读取的压缩包的路径 (本地路径)
encodingstring否统一指定读取文件的字符编码,只在 entries 值为"all"时有效。如果 entries 值为"all"且不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容
entriesFile[] or "all"是要读取的压缩包内的文件列表(当传入"all" 时表示读取压缩包内所有文件)
complete(res: TaroGeneral.CallbackResult) => void否接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: FailCallbackResult) => void否接口调用失败的回调函数
success(res: SuccessCallbackResult) => void否接口调用成功的回调函数

File​

参数类型必填说明
pathstring是压缩包内文件路径
encodingstring否指定读取文件的字符编码,如果不传 encoding,则以 ArrayBuffer 格式读取文件的二进制内容
positionnumber否从文件指定位置开始读,如果不指定,则从文件头开始读。读取的范围应该是左闭右开区间 [position, position+length)。有效范围:[0, fileLength - 1]。单位:byte
lengthnumber否指定文件的长度,如果不指定,则读到文件末尾。有效范围:[1, fileLength]。单位:byte

Encoding​

字符编码合法值

参数异常情况
ascii
base64
binary
hex
ucs2以小端序读取
ucs-2以小端序读取
utf16le以小端序读取
utf-16le以小端序读取
utf-8
utf8
latin1

FailCallbackResult​

参数类型说明
errMsgstring错误信息

可选值:
- 'fail no such file or directory, open ${filePath}': 指定的 filePath 所在目录不存在
- 'fail permission denied, open ${dirPath}': 指定的 filePath 路径没有读权限
- 'fail sdcard not mounted': Android sdcard 挂载失败

SuccessCallbackResult​

参数类型说明
entries{ [path: string]: FileItem; }文件读取结果。res.entries 是一个对象,key是文件路径,value是一个对象 FileItem ,表示该文件的读取结果。每个 FileItem 包含 data (文件内容) 和 errMsg (错误信息) 属性。

FileItem​

参数类型说明
datastring or ArrayBuffer文件内容
errMsgstring错误信息

RemoveSavedFileOption​

参数类型必填说明
filePathstring是需要删除的文件路径
apFilePathstring否本地缓存文件路径
API 支持度: alipay
complete(res: TaroGeneral.CallbackResult) => void否接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: RemoveSavedFileFailCallbackResult) => void否接口调用失败的回调函数
success(res: TaroGeneral.CallbackResult) => void否接口调用成功的回调函数

RemoveSavedFileFailCallbackResult​

参数类型说明
errMsgstring错误信息

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

RenameOption​

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

RenameFailCallbackResult​

参数类型说明
errMsgstring错误信息

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

RmdirOption​

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

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

StatOption​

参数类型必填说明
pathstring是文件/目录路径
complete(res: TaroGeneral.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​

参数类型说明
statsanyStats or Object

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

UnlinkOption​

参数类型必填说明
filePathstring是要删除的文件路径
complete(res: TaroGeneral.CallbackResult) => void否接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: UnlinkFailCallbackResult) => void否接口调用失败的回调函数
success(res: TaroGeneral.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: TaroGeneral.CallbackResult) => void否接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: UnzipFailCallbackResult) => void否接口调用失败的回调函数
success(res: TaroGeneral.CallbackResult) => void否接口调用成功的回调函数

UnzipFailCallbackResult​

参数类型说明
errMsgstring错误信息

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

WriteFileOption​

参数类型必填说明
datastring or ArrayBuffer是要写入的文本或二进制数据
filePathstring是要写入的文件路径
complete(res: TaroGeneral.CallbackResult) => void否接口调用结束的回调函数(调用成功、失败都会执行)
encodingkeyof Encoding否指定写入文件的字符编码
fail(result: WriteFileFailCallbackResult) => void否接口调用失败的回调函数
success(res: TaroGeneral.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': 存储空间不足;

FstatOption​

参数类型必填说明
fdstring是文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得
complete(res: TaroGeneral.CallbackResult) => void否接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: FstatFailCallbackResult) => void否接口调用失败的回调函数
success(res: FstatSuccessCallbackResult) => void否接口调用成功的回调函数

FstatFailCallbackResult​

参数类型说明
errMsgstring错误信息

可选值:
- 'bad file descriptor': 无效的文件描述符;
- 'fail permission denied': 指定的 fd 路径没有读权限;

FstatSuccessCallbackResult​

参数类型说明
statsStatsStats 对象,包含了文件的状态信息
errMsgstring调用结果

FstatSyncOption​

参数类型说明
fdstring文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得

CloseOption​

参数类型必填说明
fdstring是需要被关闭的文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得
complete(res: TaroGeneral.CallbackResult) => void否接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: CloseFailCallbackResult) => void否接口调用失败的回调函数
success(res: TaroGeneral.CallbackResult) => void否接口调用成功的回调函数

CloseFailCallbackResult​

参数类型说明
errMsgstring错误信息

可选值:
- 'bad file descriptor': 无效的文件描述符

CloseSyncOption​

参数类型说明
fdstring需要被关闭的文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得

FtruncateOption​

参数类型必填说明
fdstring是文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得
lengthnumber是截断位置,默认0。如果 length 小于文件长度(单位:字节),则只有前面 length 个字节会保留在文件中,其余内容会被删除;如果 length 大于文件长度,则会对其进行扩展,并且扩展部分将填充空字节('\0')
complete(res: TaroGeneral.CallbackResult) => void否接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: FtruncateFailCallbackResult) => void否接口调用失败的回调函数
success(res: TaroGeneral.CallbackResult) => void否接口调用成功的回调函数

FtruncateFailCallbackResult​

参数类型说明
errMsgstring错误信息

可选值:
- 'bad file descriptor': 无效的文件描述符
- 'fail permission denied': 指定的 fd 没有写权限
- 'fail the maximum size of the file storage limit is exceeded': 存储空间不足
- 'fail sdcard not mounted android sdcard': 挂载失败

FtruncateSyncOption​

参数类型说明
fdstring文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得
lengthnumber截断位置,默认0。如果 length 小于文件长度(单位:字节),则只有前面 length 个字节会保留在文件中,其余内容会被删除;如果 length 大于文件长度,则会对其进行扩展,并且扩展部分将填充空字节('\0')

OpenOption​

参数类型必填说明
filePathstring是文件路径 (本地路径)
flagkeyof flag否文件系统标志,默认值: 'r'
complete(res: TaroGeneral.CallbackResult) => void否接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: OpenFailCallbackResult) => void否接口调用失败的回调函数
success(res: OpenSuccessCallbackResult) => void否接口调用成功的回调函数

OpenFailCallbackResult​

参数类型说明
errMsgstring错误信息

可选值:
- 'fail no such file or directory "${filePath}"': 上级目录不存在

OpenSuccessCallbackResult​

参数类型说明
fdstring文件描述符
errMsgstring调用结果

OpenSyncOption​

参数类型必填说明
filePathstring是文件路径 (本地路径)
flagkeyof flag否文件系统标志,默认值: 'r'

ReadOption​

参数类型必填说明
fdstring是文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得
arrayBufferArrayBuffer是数据写入的缓冲区,必须是 ArrayBuffer 实例
offsetnumber否缓冲区中的写入偏移量,默认0
lengthnumber否要从文件中读取的字节数,默认0
positionnumber否文件读取的起始位置,如不传或传 null,则会从当前文件指针的位置读取。如果 position 是正整数,则文件指针位置会保持不变并从 position 读取文件。
complete(res: TaroGeneral.CallbackResult) => void否接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: ReadFailCallbackResult) => void否接口调用失败的回调函数
success(res: ReadSuccessCallbackResult) => void否接口调用成功的回调函数

ReadFailCallbackResult​

参数类型说明
errMsgstring错误信息

可选值:
- 'bad file descriptor': 无效的文件描述符
- 'fail permission denied': 指定的 fd 路径没有读权限
- 'fail the value of "offset" is out of range': 传入的 offset 不合法
- 'fail the value of "length" is out of range': 传入的 length 不合法
- 'fail sdcard not mounted': android sdcard 挂载失败
- 'bad file descriptor': 无效的文件描述符

ReadSuccessCallbackResult​

参数类型说明
bytesReadstring实际读取的字节数
arrayBufferArrayBuffer被写入的缓存区的对象,即接口入参的 arrayBuffer
errMsgstring调用结果

readCompressedFile​

Promised​

FailCallbackResult | SuccessCallbackResult

Option​

参数类型必填说明
filePathstring是要读取的文件的路径 (本地用户文件或代码包文件)
compressionAlgorithmstring是文件压缩类型,目前仅支持 'br'。
complete(res: TaroGeneral.CallbackResult) => void否接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: FailCallbackResult) => void否接口调用失败的回调函数
success(res: SuccessCallbackResult) => void否接口调用成功的回调函数

CompressionAlgorithm​

文件压缩类型合法值

参数说明
brbrotli压缩文件

FailCallbackResult​

参数类型说明
errMsgstring错误信息

可选值:
- 'fail decompress fail': 指定的 compressionAlgorithm 与文件实际压缩格式不符
- 'fail no such file or directory, open ${filePath}': 指定的 filePath 所在目录不存在
- 'fail permission denied, open ${dirPath}': 指定的 filePath 路径没有读权限

SuccessCallbackResult​

参数类型说明
dataArrayBuffer文件内容

readCompressedFileSync​

Option​

参数类型说明
filePathstring要读取的文件的路径 (本地用户文件或代码包文件)
compressionAlgorithmstring文件压缩类型,目前仅支持 'br'。

CompressionAlgorithm​

文件压缩类型合法值

参数说明
brbrotli压缩文件

ReadSyncOption​

参数类型必填说明
fdstring是文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得
arrayBufferArrayBuffer是数据写入的缓冲区,必须是 ArrayBuffer 实例
offsetnumber否缓冲区中的写入偏移量,默认0
lengthnumber否要从文件中读取的字节数,默认0
positionnumber否文件读取的起始位置,如不传或传 null,则会从当前文件指针的位置读取。如果 position 是正整数,则文件指针位置会保持不变并从 position 读取文件。

TruncateOption​

参数类型必填说明
filePathstring是要截断的文件路径 (本地路径)
lengthnumber否截断位置,默认0。如果 length 小于文件长度(字节),则只有前面 length 个字节会保留在文件中,其余内容会被删除;如果 length 大于文件长度,则会对其进行扩展,并且扩展部分将填充空字节('\0')
complete(res: TaroGeneral.CallbackResult) => void否接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: TruncateFailCallbackResult) => void否接口调用失败的回调函数
success(res: TaroGeneral.CallbackResult) => void否接口调用成功的回调函数

TruncateFailCallbackResult​

参数类型说明
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 the maximum size of the file storage limit is exceeded': 存储空间不足
- 'fail sdcard not mounted': android sdcard 挂载失败

TruncateSyncOption​

参数类型必填说明
filePathstring是要截断的文件路径 (本地路径)
lengthnumber否截断位置,默认0。如果 length 小于文件长度(字节),则只有前面 length 个字节会保留在文件中,其余内容会被删除;如果 length 大于文件长度,则会对其进行扩展,并且扩展部分将填充空字节('\0')

WriteOption​

参数类型必填说明
fdstring是文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得
datastring or ArrayBuffer是写入的内容,类型为 String 或 ArrayBuffer
offsetnumber否只在 data 类型是 ArrayBuffer 时有效,决定 arrayBuffe 中要被写入的部位,即 arrayBuffer 中的索引,默认0
lengthnumber否只在 data 类型是 ArrayBuffer 时有效,指定要写入的字节数,默认为 arrayBuffer 从0开始偏移 offset 个字节后剩余的字节数
encodingkeyof Encoding否只在 data 类型是 String 时有效,指定写入文件的字符编码,默认为 utf8
positionnumber否指定文件开头的偏移量,即数据要被写入的位置。当 position 不传或者传入非 Number 类型的值时,数据会被写入当前指针所在位置。
complete(res: TaroGeneral.CallbackResult) => void否接口调用结束的回调函数(调用成功、失败都会执行)
fail(result: WriteFailCallbackResult) => void否接口调用失败的回调函数
success(res: WriteSuccessCallbackResult) => void否接口调用成功的回调函数

WriteFailCallbackResult​

参数类型说明
errMsgstring错误信息

可选值:
'bad file descriptor': 无效的文件描述符
'fail permission denied': 指定的 fd 路径没有写权限
'fail sdcard not mounted': android sdcard 挂载失败

WriteSuccessCallbackResult​

参数类型说明
bytesWrittennumber实际被写入到文件中的字节数(注意,被写入的字节数不一定与被写入的字符串字符数相同)
errMsgstring调用结果

WriteSyncOption​

参数类型必填说明
fdstring是文件描述符。fd 通过 FileSystemManager.open 或 FileSystemManager.openSync 接口获得
datastring or ArrayBuffer是写入的内容,类型为 String 或 ArrayBuffer
offsetnumber否只在 data 类型是 ArrayBuffer 时有效,决定 arrayBuffe 中要被写入的部位,即 arrayBuffer 中的索引,默认0
lengthnumber否只在 data 类型是 ArrayBuffer 时有效,指定要写入的字节数,默认为 arrayBuffer 从0开始偏移 offset 个字节后剩余的字节数
encodingkeyof Encoding否只在 data 类型是 String 时有效,指定写入文件的字符编码,默认为 utf8
positionnumber否指定文件开头的偏移量,即数据要被写入的位置。当 position 不传或者传入非 Number 类型的值时,数据会被写入当前指针所在位置。

API 支持度​

API微信小程序百度小程序支付宝小程序抖音小程序QQ 小程序京东小程序H5React NativeHarmonyHarmony hybridASCF元服务
FileSystemManager✔️✔️✔️✔️
FileSystemManager.access✔️✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.accessSync✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.appendFile✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.appendFileSync✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.close✔️✔️
FileSystemManager.closeSync✔️✔️
FileSystemManager.copyFile✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.copyFileSync✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.fstat✔️✔️
FileSystemManager.fstatSync✔️✔️
FileSystemManager.ftruncate✔️✔️
FileSystemManager.ftruncateSync✔️✔️
FileSystemManager.getFileInfo✔️✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.getSavedFileList✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.mkdir✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.mkdirSync✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.open✔️✔️
FileSystemManager.openSync✔️✔️
FileSystemManager.read✔️✔️
FileSystemManager.readCompressedFile✔️
FileSystemManager.readCompressedFileSync✔️
FileSystemManager.readdir✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.readdirSync✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.readFile✔️✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.readFileSync✔️✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.readSync✔️✔️
FileSystemManager.readZipEntry✔️
FileSystemManager.removeSavedFile✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.rename✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.renameSync✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.rmdir✔️✔️✔️
FileSystemManager.rmdirSync✔️✔️✔️
FileSystemManager.saveFile✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.saveFileSync✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.stat✔️✔️✔️✔️✔️✔️
FileSystemManager.statSync✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.truncate✔️✔️✔️
FileSystemManager.truncateSync✔️✔️✔️
FileSystemManager.unlink✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.unlinkSync✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.unzip✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.write✔️✔️
FileSystemManager.writeFile✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.writeFileSync✔️✔️✔️✔️✔️✔️✔️
FileSystemManager.writeSync✔️✔️