# FileSystemManager.writeFile(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
Write file
# parameter
# Object object
| attribute | type | Default values | Required | Introductions |
|---|---|---|---|---|
| filePath | string | yes | File path to write to (Local path) | |
| data | string/ArrayBuffer | yes | Text or binary data to write | |
| encoding | string | utf8 | no | Specifies the character encoding for writing to the file |
| 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.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 does not have write permissions | |
| fail the maximum size of the file storage limit is exceeded | Insufficient storage space | |
| fail sdcard not mounted | Android sdcard Mount failure |
# sample code
const fs = wx.getFileSystemManager()
fs.writeFile({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
data: 'some text or arrayBuffer',
encoding: 'utf8',
success(res) {
console.log(res)
},
fail(res) {
console.error(res)
}
})
// Synchronous interface
try {
const res = fs.writeFileSync(
`${wx.env.USER_DATA_PATH}/hello.txt`,
'some text or arrayBuffer',
'utf8'
)
console.log(res)
} catch(e) {
console.error(e)
}