FileSystemManager.readFile
Introduction
Reads the contents of a local file. The maximum file size limit is 100MB.
Usage Restrictions
Supported in basic library version 3.0.0 or higher.
Parameters
Object object
Attribute | Type | Default | Required | Description |
---|---|---|---|---|
filePath | string | Yes | The path of the file to be read (if you are reading a directory within the applet, such as the images in the images directory under the root directory, you can pass "images/xx.png") | |
encoding | string | No | Specifies the character encoding of the file to be read. If encoding is not provided, the file's binary content is read as an ArrayBuffer. Valid values: 1. ascii 2. base64 3. binary 4. hex 5. ucs2, read in little-endian 6. ucs-2, read in little-endian 7. utf16le, read in little-endian 8. utf-16le, read in little-endian 9. utf-8 10. utf8 11. latin1 | |
length | number | No | Specifies the length of the file to be read. If not specified, the file is read until the end. Valid range: [1, fileLength]. Unit: byte | |
position | number | No | Reads from the specified position in the file. If not specified, reading starts from the beginning of the file. The range of the read should be a left-closed right-open interval [position, position+length). Valid range: [0, fileLength - 1]. Unit: byte | |
success | function | No | Callback function called when the interface is successfully invoked. | |
fail | function | No | Callback function called when the interface fails to be invoked. | |
complete | function | No | Callback function called when the interface invocation ends (executed regardless of success or failure). |
object.success
Callback Function
Parameters
Object res
Attribute | Type | Description |
---|---|---|
data | string/ArrayBuffer | File content |
success | string | true - success |
object.fail Callback Function
Parameters
Object res
Attribute | Type | Description |
---|---|---|
errMsg | string | Error message |
success | string | false - failure |
errMsg Description
Error Message | Description |
---|---|
parameter error: F10001 | Invalid parameters |
fail sdcard not mounted: F10002 | Android sdcard mounting fail |
fail no such file or directory: F10007 ${filePath} | Directory of filePath does not exist |
native buffer exceeds size limit: F10008 | File size exceeds limit (100MB) |
the named is ${encoding} charset is not supported! | Current character encoding is not supported |
Sample Code
// pages/index/index.js
Page({
readFile() {
const fs = dlt.getFileSystemManager();
fs.readFile({
filePath: `${dlt.env.USER_DATA_PATH}/hello.txt`,
encoding: 'utf8',
position: 0,
success(res) {
console.log(res.data);
},
fail(res) {
console.error(res);
},
});
},
});