Called in Promise style: Not supported
Mini Program plugin: Supported, requires Mini Program base library version not lower than 2.19.2
WeChat HarmonyOS Edition: Supported
Related documentation: File System
Feature Description
The synchronous version of FileSystemManager.writeFile.
Parameters
string filePath
The file path to be written to (local path)
string|ArrayBuffer data
The text or binary data to be written
string encoding
Specifies the character encoding for writing to the file
Valid values for encoding
| Value | Description | Minimum Version |
|---|---|---|
| ascii | ||
| base64 | (Note: If base64 encoding is selected, the data field should only contain the base64 content itself. Do not include the Data URI prefix, otherwise a "fail base64 encode error" will occur. For example, pass aGVsbG8= instead of data:image/png;base64,aGVsbG8=) | |
| binary | ||
| hex | ||
| ucs2 | Read as little-endian | |
| ucs-2 | Read as little-endian | |
| utf16le | Read as little-endian | |
| utf-16le | Read as little-endian | |
| utf-8 | ||
| utf8 | ||
| latin1 |
Errors
| Error Code | Error Message | Description |
|---|---|---|
| 1300001 | operation not permitted | Operation not allowed (e.g., expected a file for filePath but a directory was provided) |
| 1300002 | no such file or directory ${path} | File/directory does not exist, or the parent directory of the target file path does not exist |
| 1300005 | Input/output error | Input/output stream unavailable |
| 1300009 | bad file descriptor | Invalid file descriptor |
| 1300013 | permission denied | Permission error, file is read-only or write-only |
| 1300014 | Path permission denied | No permission for the provided path |
| 1300020 | not a directory | The path specified by dirPath is not a directory, common when the parent path of the specified write path is a file |
| 1300021 | Is a directory | The specified path is a directory |
| 1300022 | Invalid argument | Invalid argument, check if length or offset is out of bounds |
| 1300036 | File name too long | File name is too long |
| 1300066 | directory not empty | Directory is not empty |
| 1300201 | system error | System interface call failed |
| 1300202 | the maximum size of the file storage limit is exceeded | Insufficient storage space, or file size exceeds the limit (limit 100M) |
| 1300203 | base64 encode error | Character encoding conversion failed (e.g., incorrect base64 format) |
| 1300300 | sdcard not mounted | Android SD card mount failed |
| 1300301 | unable to open as fileType | Unable to open file as fileType |
| 1301000 | permission denied, cannot access file path | No access permission to the target path (usr directory) |
| 1301002 | data to write is empty | Data to write is empty |
| 1301003 | illegal operation on a directory | This operation is not allowed on a directory (e.g., specified filePath is an existing directory) |
| 1301004 | illegal operation on a package directory | This operation is not allowed on a package directory |
| 1301005 | file already exists ${dirPath} | A file or directory with the same name already exists |
| 1301006 | value of length is out of range | The provided length is invalid |
| 1301007 | value of offset is out of range | The provided offset is invalid |
| 1301009 | value of position is out of range | position value is out of range |
| 1301100 | store directory is empty | store directory is empty |
| 1301102 | unzip open file fail | Failed to open compressed file |
| 1301103 | unzip entry fail | Failed to extract a single file |
| 1301104 | unzip fail | Extraction failed |
| 1301111 | brotli decompress fail | brotli decompression failed (e.g., specified compressionAlgorithm does not match the actual file compression format) |
| 1301112 | tempFilePath file not exist | File not found at specified tempFilePath |
| 1302001 | fail permission denied | No read/write permission for the specified fd path |
| 1302002 | excced max concurrent fd limit | Maximum number of concurrent fds reached |
| 1302003 | invalid flag | Invalid flag |
| 1302004 | permission denied when open using flag | Cannot open file using the specified flag |
| 1302005 | array buffer does not exist | arrayBuffer not provided |
| 1302100 | array buffer is readonly | arrayBuffer is read-only |
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)
}
})
// 同步接口
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)
}