Called in Promise style: Not supported
Mini Program plugin: Supported, requires Mini Program base library version not lower than 2.19.2
WeChat for Harmony OS: Supported
Related documentation: File System
Feature Description
Synchronous version of FileSystemManager.readFile
Parameters
string filePath
The path of the file to be read (local path)
string encoding
Specifies the character encoding for reading the file. If encoding is not specified, the file's binary content is read as an ArrayBuffer.
Valid values for encoding
| Value | Description | Minimum Version |
|---|---|---|
| ascii | ||
| base64 | ||
| binary | ||
| hex | ||
| ucs2 | Read in little-endian order | |
| ucs-2 | Read in little-endian order | |
| utf16le | Read in little-endian order | |
| utf-16le | Read in little-endian order | |
| utf-8 | ||
| utf8 | ||
| latin1 |
number position
Base Library 2.10.0 or later is required. For earlier versions, see compatibility handling.
Reads from the specified position in the file. If not specified, it starts from the beginning of the file. The read range is a left-closed, right-open interval [position, position+length). Valid range: [0, fileLength - 1]. Unit: byte
Number Length
Base Library 2.10.0 or later is required. For earlier versions, see compatibility handling.
Specifies the length of the file to read. If not specified, it reads until the end of the file. Valid range: [1, fileLength]. Unit: byte
Return Values
string|ArrayBuffer data
File content
Error
| Error Code | Error Message | Description |
|---|---|---|
| 1300001 | operation not permitted | Operation is not permitted (e.g., a directory was passed as filePath while a file was expected) |
| 1300002 | no such file or directory ${path} | The file/directory does not exist, or the parent directory of the target file path does not exist |
| 1300005 | Input/output error | Input/output stream is unavailable |
| 1300009 | bad file descriptor | Invalid file descriptor |
| 1300013 | permission denied | Permission error; the file is read-only or write-only |
| 1300014 | Path permission denied | The provided path lacks the required permissions |
| 1300020 | not a directory | The path specified by dirPath is not a directory; this commonly occurs when the parent directory of the specified write path is a file |
| 1300021 | Is a directory | The specified path is a directory |
| 1300022 | Invalid argument | Invalid argument; check if length or offset is out of bounds |
| 1300036 | File name too long | The file name is too long |
| 1300066 | directory not empty | The directory is not empty |
| 1300201 | system error | System interface call failed |
| 1300202 | the maximum size of the file storage limit is exceeded | Insufficient storage space, or file size exceeds the limit (limit is 100MB) |
| 1300203 | base64 encode error | Character encoding conversion failed (e.g., incorrect base64 format) |
| 1300300 | sdcard not mounted | Android SD card mounting failed |
| 1300301 | unable to open as fileType | Unable to open the file with the specified fileType |
| 1301000 | permission denied, cannot access file path | No access permission to the target path (usr directory) |
| 1301002 | data to write is empty | The data to be written is empty |
| 1301003 | illegal operation on a directory | This operation is not allowed on a directory (e.g., the specified filePath is an existing directory) |
| 1301004 | illegal operation on a package directory | This operation is not allowed on the code package directory |
| 1301005 | file already exists ${dirPath} | A file or directory with the same name already exists |
| 1301006 | value of length is out of range | The provided length is invalid |
| 1301007 | value of offset is out of range | The provided offset is invalid |
| 1301009 | value of position is out of range | The value of position is out of bounds |
| 1301100 | store directory is empty | The store directory is empty |
| 1301102 | unzip open file fail | Failed to open the compressed file |
| 1301103 | unzip entry fail | Failed to extract a single file |
| 1301104 | unzip fail | Failed to extract the archive |
| 1301111 | brotli decompress fail | Brotli decompression failed (e.g., the specified compressionAlgorithm does not match the actual compression format of the file) |
| 1301112 | tempFilePath file not exist | The file at the specified tempFilePath cannot be found |
| 1302001 | fail permission denied | No read/write permission to the specified fd path |
| 1302002 | excced max concurrent fd limit | The number of file descriptors has reached the limit |
| 1302003 | invalid flag | Invalid flag |
| 1302004 | permission denied when open using flag | Unable to open the file using the specified flag |
| 1302005 | array buffer does not exist | arrayBuffer was not provided |
| 1302100 | array buffer is readonly | arrayBuffer is read-only |
Sample Code
const fs = wx.getFileSystemManager()
fs.readFile({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
encoding: 'utf8',
position: 0,
success(res) {
console.log(res.data)
},
fail(res) {
console.error(res)
}
})
// 同步接口
try {
const res = fs.readFileSync(`${wx.env.USER_DATA_PATH}/hello.txt`, 'utf8', 0)
console.log(res)
} catch(e) {
console.error(e)
}