# string|ArrayBuffer FileSystemManager.readFileSync(string filePath, string encoding, number position, number length)
with Promise style call: Not supported
Mini Program plugin: Support, need to Mini Program base library version no less than 2.19.2
FileSystemManager.readFile The synchronous version of
# parameter
# string filePath
Path to the file to read (Local path)
# string encoding
Specifies the character encoding for the read file, if the Encoding, the ArrayBuffer Format to read the binary contents of a file
encoding Legal value
value | Introductions | Minimum version |
---|---|---|
ascii | ||
base64 | ||
binary | ||
hex | ||
ucs2 | Read in small endorder | |
ucs-2 | Read in small endorder | |
Utf16le | Read in small endorder | |
utf-16le | Read in small endorder | |
utf-8 | ||
utf8 | ||
latin1 |
# number position
Start from base library version 2.10.0. Please remaining backward compatible.
Start reading from the specified location in the file or from the file header if not specified. Read range should be left closed and right open range [position, position+length)。有效范围:[0, fileLength - 1]. Unit: byte
# number length
Start from base library version 2.10.0. Please remaining backward compatible.
Specifies the length of the file, if not specified, read to the end of the document. Scope of validity:[1, fileLength]. Unit: byte
# Return value
# string|ArrayBuffer data
Document content
# error
Error code | Error message | Introductions |
---|---|---|
fail No such file or directory, open ${filePath} | Specified filePath Directory does not exist | |
fail permission denied, open ${dirPath} | Specified filePath Path not read | |
fail sdcard not mounted | Android sdcard Mount failure |
# 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)
}
})
// Synchronous interface
try {
const res = fs.readFileSync(`${wx.env.USER_DATA_PATH}/hello.txt`, 'utf8', 0)
console.log(res)
} catch(e) {
console.error(e)
}