HomeDocs
Skip to main content

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

AttributeTypeDefaultRequiredDescription
filePathstringYesThe 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")
encodingstringNoSpecifies 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
lengthnumberNoSpecifies the length of the file to be read. If not specified, the file is read until the end.
Valid range: [1, fileLength].
Unit: byte
positionnumberNoReads 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
successfunctionNoCallback function called when the interface is successfully invoked.
failfunctionNoCallback function called when the interface fails to be invoked.
completefunctionNoCallback function called when the interface invocation ends (executed regardless of success or failure).

object.success

Callback Function

Parameters

Object res

AttributeTypeDescription
datastring/ArrayBufferFile content
successstringtrue - success

object.fail Callback Function

Parameters

Object res

AttributeTypeDescription
errMsgstringError message
successstringfalse - failure

errMsg Description

Error MessageDescription
parameter error: F10001Invalid parameters
fail sdcard not mounted: F10002Android sdcard mounting fail
fail no such file or directory: F10007 ${filePath}Directory of filePath does not exist
native buffer exceeds size limit: F10008File 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);
},
});
},
});
Privacy agreementDeveloper agreementcontact us: developer_service.mi@transsion.com © 2024 MiniApp. All Rights Reserved.