# FileSystemManager.readFile(Object object)
with Promise style call: Not supported
Mini Program plugin: Support, need to Mini Program base library version no less than 2.19.2
Read local file content
# parameter
# Object object
attribute | type | Default values | Required | Introductions | Minimum version |
---|---|---|---|---|---|
filePath | string | yes | Path to the file to read (Local path) | ||
encoding | string | no | Specifies the character encoding for the read file, if the Encoding, the ArrayBuffer Format to read the binary contents of a file | ||
position | number | no | 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 | 2.10.0 | |
length | number | no | Specifies the length of the file, if not specified, read to the end of the document. Scope of validity:[1, fileLength]. Unit: byte | 2.10.0 | |
success | function | no | Interface calls the successful callback function | ||
fail | function | no | Interface calls failed callback functions | ||
complete | function | no | Callback function at the end of an interface call (both successful and unsuccessful calls are executed) |
object.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 |
# object.success callback
# parameter
# Object res
attribute | type | Introductions |
---|---|---|
data | string/ArrayBuffer | Document content |
# object.fail callback
# parameter
# Object res
attribute | type | Introductions |
---|---|---|
errMsg | string | Error message |
res.errMsg Legal value
value | Introductions | Minimum version |
---|---|---|
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)
}