FileSystemManager.writeFile
Introduction
Writes a file.
Usage Restrictions
Supported in basic library version 3.0.0 or higher.
Parameters
Object object
| Property | Type | Default | Required | Description |
|---|---|---|---|---|
| filePath | string | yes | The file path (relative path) to write to. | |
| data | string | yes | The content to be written, as a String (ArrayBuffer is not supported at the moment). | |
| encoding | string | utf8 | no | Only 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 |
| success | function | no | Callback function to be called when the interface call is successful. | |
| fail | function | no | Callback function to be called when the interface call fails. | |
| complete | function | no | Callback function to be called when the interface call ends (executed whether successful or failed). |
object.success
Callback Function
Parameters
Object res
| Property | Type | Description |
|---|---|---|
| bytesWritten | number | The 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). |
| success | string | true - set successfully |
object.fail
Callback Function
Parameters
Object res
| Property | Type | Description |
|---|---|---|
| errMsg | string | Error message |
| success | string | false - failed |
errMsg Description
| Error message | Description |
|---|---|
| parameter error: F10001 | Invalid parameter |
| fail sdcard not mounted: F10002 | Failed to mount Android sdcard |
| fail the maximum size of the file storage limit exceeded: F10004 | Insufficient 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);
},
});
},
});