HomeDocs
Skip to main content

FileSystemManager.writeFile

Introduction

Writes a file.

Usage Restrictions

Supported in basic library version 3.0.0 or higher.

Parameters

Object object

PropertyTypeDefaultRequiredDescription
filePathstringyesThe file path (relative path) to write to.
datastringyesThe content to be written, as a String (ArrayBuffer is not supported at the moment).
encodingstringutf8noOnly effective when the data type is String, specifies the character encoding for writing the file. Default is utf8.
Valid values:
1. ascii
2. base64
3. binary
4. hex
5. ucs2 little endian
6. ucs-2 little endian
7. utf16le little endian
8. utf-16le little endian
9. utf-8
10. utf8
11. latin1
successfunctionnoCallback function to be called when the interface call is successful.
failfunctionnoCallback function to be called when the interface call fails.
completefunctionnoCallback function to be called when the interface call ends (executed whether successful or failed).

object.success

Callback Function

Parameters

Object res

PropertyTypeDescription
bytesWrittennumberThe actual number of bytes written to the file (note that the number of bytes written may not be the same as the number of characters in the written string).
successstringtrue - set successfully

object.fail

Callback Function

Parameters

Object res

PropertyTypeDescription
errMsgstringError message
successstringfalse - failed

errMsg Description

Error messageDescription
parameter error: F10001Invalid parameter
fail sdcard not mounted: F10002Failed to mount Android sdcard
fail the maximum size of the file storage limit exceeded: F10004Insufficient storage space
fail permission denied: F10005, open ${filePath}No read permission for filePath
fail no such file or directory: F10007,open ${filePath}Directory of filePath does not exist
the named is ${encoding} charset is not supported!Current character encoding is not supported

Sample Code

// pages/index/index.js
Page({
writeFile() {
const fs = dlt.getFileSystemManager();
fs.writeFile({
filePath: `${dlt.env.USER_DATA_PATH}/hello.txt`,
data: 'some text',
encoding: 'utf8',
success(res) {
console.log(res);
},
fail(res) {
console.error(res);
},
});
},
});
Privacy agreementDeveloper agreementcontact us: developer_service.mi@transsion.com © 2024 MiniApp. All Rights Reserved.