tencent cloud

All product documents
Tencent Cloud Super App as a Service
Last updated: 2025-03-25 18:15:56
File
Last updated: 2025-03-25 18:15:56

saveFile

This API is called using wx.saveFile(Object object).
Note:
This API is supported in mini programs but not in mini games.
Note:
The size limit for local file storage is 10 MB.
Feature description:This API saves a file to the local storage.
Note:
The saveFile moves the temporary file, so the tempFilePath provided will no longer be available after a successful call.
Parameter and description: Object object
Property
Type
Default value
‍Required
Description
tempFilePath
string
-
True
The temporary path of the file to be saved.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
object.success callback function parameter:Object res
Property
Type
Description
savedFilePath
string
The path of the file after saving.
Example:
wx.chooseImage({
success(res) {
const tempFilePaths = res.tempFilePaths
wx.saveFile({
tempFilePath: tempFilePaths[0],
success(res) {
const savedFilePath = res.savedFilePath
}
})
}
})

removeSavedFile

This API is called using wx.removeSavedFile(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Deletes local cache files.
Parameter and description: Object object
Property
Type
Default value
‍Required
Description
filePath
string
-
True
The path to the file that needs to be deleted.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
Example:
wx.getSavedFileList({
success(res) {
if (res.fileList.length > 0) {
wx.removeSavedFile({
filePath: res.fileList[0].filePath,
complete(res) {
console.log(res)
}
})
}
}
})

openDocument

This API is called using wx.openDocument(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Opens a document in a new page.
Parameter and description: Object object
Property
Type
Default value
‍Required
Description
filePath
string
-
True
The file path (local path), which can be obtained via downloadFile.
fileType
string
-
False
The type of the file, used to specify the file type for opening.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
Valid values for object.fileType
Value
Description
pdf
PDF format.
Example:
wx.downloadFile({
// Example URL, not a real file
url: 'https://example.com/somefile.pdf',
success(res) {
const filePath = res.tempFilePath
wx.openDocument({
filePath,
success(res) {
console.log('Document opened successfully')
}
})
}
})

getSavedFileList

This API is called using wx.getSavedFileList(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Gets the list of saved local cache files in this mini program.
Parameter and description: Object object
Property
Type
Default value
‍Required
Description
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
object.success callback function parameter: Object res
Property
Type
Description
fileList
Array.<Object>
Array of files, each item is a FileItem.
res.fileList structure
Property
Type
Description
filePath
string
Local path.
size
number
Local file size in bytes.
createTime
number
Timestamp when the file was saved, in seconds since 1970/01/01 08:00:00
Example:
wx.getSavedFileList({
success(res) {
console.log(res.fileList)
}
})

getSavedFilelnfo

This API is called using wx.getSavedFileInfo(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Gets the information of a local file. This API can only be used to get information about files that have been saved locally. For temporary files, use the wx.getFileInfo() .
Parameter and description: Object object
Property
Type
Default value
‍Required
Description
filePath
string
-
True
File path.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
object.success callback function parameter:Object res
Property
Type
Description
size
number
File size in bytes.
createTime
number
Timestamp when the file was saved, in seconds since 1970/01/01 08:00:00
Example:
wx.getSavedFileList({
success(res) {
console.log(res.fileList)
}
})

getFileInfo

This API is called using wx.getFileInfo(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Gets the file information.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
filePath
string
-
True
Local file path.
digestAlgorithm
string
'md5'
False
The algorithm used to calculate the file digest.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
Valid values for object.digestAlgorithm
Value
Description
md5
The MD5 algorithm.
sha1
The SHA1 algorithm.
object.success callback function parameter:Object res
Property
Type
Description
size
number
The file size in bytes.
digest
string
File digest calculated using the specified algorithm.
Example:
wx.getFileInfo({
success(res) {
console.log(res.size)
console.log(res.digest)
}
})

getFileSystemManager

This API is called using FileSystemManager wx.getFileSystemManager().
Feature description:Gets a globally unique file manager.
Return value: FileSystemManager , the file manager.

FileSystemManager

File manager, which can be obtained using wx.getFileSystemManager .

.access

This method is called using FileSystemManager.access(Object object).
Feature description:Determines whether a file or directory exists.
Parameter and description: Object object
Property
Type
Default value
‍Required
Description
path
string
-
True
The path of the file or directory to check for existence.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
object.fail callback function parameter:Object res
Property
Type
Description
errMsg
string
Error message.
Valid values for res.errMsg
Value
Description
fail no such file or directory ${path}
The file or directory does not exist.
bad file descriptor
Invalid file descriptor.
permission denied
Permission error. The file is read-only or write-only.
permission denied, cannot access file path
No access to the target path (e.g., usr directory)
not a directory
The specified path is not a directory
Invalid argument
Invalid argument, check if length or offset is out of range.
directory not empty
The directory is not empty.
the maximum size of the file storage limit is exceeded
Storage space is insufficient, or file size exceeds the limit (100 MB).
base64 encode error
Character encoding conversion failed (e.g., incorrect base64 format).
data to write is empty
The data to write is empty.
illegal operation on a directory
Illegal operation on a directory (e.g., specified filePath is an existing directory).
file already exists ${dirPath}
A file or directory with the same name already exists.
value of length is out of range
The length value is out of range.
value of offset is out of range
The offset value is out of range.
value of position is out of range
The position value is out of range.
Example:
const fs = wx.getFileSystemManager()
// Determine whether a file or directory exists
fs.access({
path: `${wx.env.USER_DATA_PATH}/hello.txt`,
success(res) {
// The file exists
console.log(res)
},
fail(res) {
// File does not exist or other error
console.error(res)
}
})

// Synchronous API
try {
fs.accessSync(`${wx.env.USER_DATA_PATH}/hello.txt`)
} catch(e) {
console.error(e)
}

.accessSync

This method is called using FileSystemManager.accessSync(string path).
Feature description:Synchronously performs the same feature as FileSystemManager.access.
Parameter and description: string pathThe path of the file or directory to check for existence.
Example:
const fs = wx.getFileSystemManager()
// Determine whether a file or directory exists
fs.access({
path: `${wx.env.USER_DATA_PATH}/hello.txt`,
success(res) {
// The file exists
console.log(res)
},
fail(res) {
// File does not exist or other error
console.error(res)
}
})

// Synchronous API
try {
fs.accessSync(`${wx.env.USER_DATA_PATH}/hello.txt`)
} catch(e) {
console.error(e)
}

.appendFile

This method is called using FileSystemManager.appendFile(Object object).
Feature description:Appends content to the end of a file.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
filePath
string
-
True
Path of the file to append content to.
data
string/ArrayBuffer
-
True
Text or binary data to append.
encoding
string
utf-8
False
Character encoding for writing to the file.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
Valid values for object.encoding
Value
Description
ascii
-
base64
-
binary
-
hex
-
ucs2/ucs-2/utf16le/utf-16le
Reads in little-endian order.
utf-8/utf8
-
latin1
-
object.fail callback function parameter:Object res
Property
Type
Description
errMsg
string
Error message.
Valid values for res.errMsg
Value
Description
fail no such file or directory, open ${filePath}
The specified filePath does not exist.
fail illegal operation on a directory, open "${filePath}"
The specified filePath is a directory that already exists.
fail permission denied, open ${dirPath}
No write permission for the specified filePath.
fail sdcard not mounted
Failed to mount the Android SDCard.
Example:
const fs = wx.getFileSystemManager()

fs.appendFile({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
data: 'some text',
encoding: 'utf8',
success(res) {
console.log(res)
},
fail(res) {
console.error(res)
}
})

// Synchronous API
try {
fs.appendFileSync(`${wx.env.USER_DATA_PATH}/hello.txt`, 'some text', 'utf8')
} catch(e) {
console.error(e)
}

.appendFileSync

This method is called using FileSystemManager.appendFileSync(string filePath, string|ArrayBuffer data, string encoding).
Feature description:Synchronously performs the same feature as FileSystemManager.appendFile.
Parameter and description
string filePath: Path of the file to append content to.
string|ArrayBuffer data: Text or binary data to append.
string encoding: Character encoding for writing to the file.
Valid values for encoding
Value
Description
ascii
-
base64
-
binary
-
hex
-
ucs2/ucs-2/utf16le/utf-16le
Reads in little-endian order.
utf-8/utf8
-
latin1
-
Example:
const fs = wx.getFileSystemManager()

fs.appendFile({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
data: 'some text',
encoding: 'utf8',
success(res) {
console.log(res)
},
fail(res) {
console.error(res)
}
})

// Synchronous API
try {
fs.appendFileSync(`${wx.env.USER_DATA_PATH}/hello.txt`, 'some text', 'utf8')
} catch(e) {
console.error(e)
}

.close

This method is called using FileSystemManager.close(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Closes a file.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
fd
string
-
True
File descriptor to be closed, obtained through FileSystemManager.open or FileSystemManager.openSync.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
Example
const fs = wx.getFileSystemManager()
// Open a file
fs.open({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
flag: 'a+',
success(res) {
// Close the file
fs.close({
fd: res.fd
})
}
})

.closeSync

This method is called using FileSystemManager.closeSync(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Synchronously closes a file.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
fd
string
-
True
File descriptor to be closed, obtained through FileSystemManager.open or FileSystemManager.openSync.
Return value:undefined.
Example
const fs = wx.getFileSystemManager()
const fd = fs.openSync({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
flag: 'a+'
})

// Close the file
fs.closeSync({fd: fd})

.copyFile

This method is called using FileSystemManager.copyFile(Object object).
Feature description:Copies a file.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
srcPath
string
-
True
Source file path, must be a regular file.
destPath
string
-
True
Destination file path, supports local paths.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
object.fail callback function parameter:Object res
Property
Type
Description
errMsg
string
Error message.
Valid values for res.errMsg
Value
Description
fail permission denied, copyFile ${srcPath} -> ${destPath}
No write permission for the destination file path.
fail no such file or directory, copyFile ${srcPath} -> ${destPath}
Source file does not exist, or the parent directory of the destination file path does not exist.
Example:
const fs = wx.getFileSystemManager()
fs.copyFile({
srcPath: `${wx.env.USER_DATA_PATH}/hello.txt`,
destPath: `${wx.env.USER_DATA_PATH}/hello_copy.txt`
success(res) {
console.log(res)
},
fail(res) {
console.error(res)
}
})

// Synchronous API
try {
fs.copyFileSync(
`${wx.env.USER_DATA_PATH}/hello.txt`,
`${wx.env.USER_DATA_PATH}/hello_copy.txt`
)
} catch(e) {
console.error(e)
}

.copyFileSync

This method is called using FileSystemManager.copyFileSync(string srcPath, string destPath).
Feature description:Synchronously performs the same feature as FileSystemManager.copyFile.
Parameter and description:
string srcPath: Source file path, must be a regular file.
string destPath: Destination file path.
Example:
const fs = wx.getFileSystemManager()
fs.copyFile({
srcPath: `${wx.env.USER_DATA_PATH}/hello.txt`,
destPath: `${wx.env.USER_DATA_PATH}/hello_copy.txt`
success(res) {
console.log(res)
},
fail(res) {
console.error(res)
}
})

// Synchronous API
try {
fs.copyFileSync(
`${wx.env.USER_DATA_PATH}/hello.txt`,
`${wx.env.USER_DATA_PATH}/hello_copy.txt`
)
} catch(e) {
console.error(e)
}

.getFileInfo

This method is called using FileSystemManager.getFileInfo(Object object).
Feature description:Gets the information of local temporary file or local cache file in this mini program.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
filePath
string
-
True
The path of the file to read.
digestAlgorithm
string
md5
False
The algorithm used to calculate the file digest.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
Valid values for object.digestAlgorithm
Value
Description
md5
The MD5 algorithm.
sha1
The SHA1 algorithm.
object.success callback function parameter:Object res
Property
Type
Description
size
number
The file size in bytes.
digest
string
File digest calculated using the specified algorithm.
object.fail callback function parameter:Object res
Property
Type
Description
errMsg
string
Error message.
Valid values for res.errMsg
Value
Description
fail file not exist
The specified filePath could not be found.
no such file or directory ${path}
File/directory does not exist, or the parent directory is missing.
Input/output error
Input/output stream is unavailable.
permission denied
Permission error. The file is read-only or write-only.
Path permission denied
No permission for the specified path.
not a directory
The specified path is not a directory.
Invalid argument
Invalid argument, check if length or offset is out of range.
excced max concurrent fd limit
Maximum number of file descriptors reached.

.fstat

This method is called using FileSystemManager.fstat(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Gets status information of a file.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
fd
string
-
True
File descriptor obtained through FileSystemManager.open or FileSystemManager.openSync.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
object.success callback function parameter:Object res
Property
Type
Description
stats
Stats
Stats object containing file status information.
Example:
const fs = wx.getFileSystemManager()
// Open a file
fs.open({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
flag: 'a+',
success(res) {
// Get status information of the file
fs.fstat({
fd: res.fd,
success(res) {
console.log(res.stats)
}
})
}
})

.fstatSync

This method is called using Stats FileSystemManager.fstatSync(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Synchronously gets status information of a file.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
fd
string
-
True
File descriptor obtained through FileSystemManager.open or FileSystemManager.openSync.
Return value:Stats Stats object containing file status information.
Example:
const fs = wx.getFileSystemManager()
const fd = fs.openSync({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
flag: 'a+'
})
const stats = fs.fstatSync({fd: fd})
console.log(stats)

.ftruncate

This method is called using FileSystemManager.ftruncate(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Truncates the file content.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
fd
string
-
True
File descriptor obtained through FileSystemManager.open or FileSystemManager.openSync.
length
number
-
True
The truncation position. Default value: 0. If the length is less than the file length (in bytes), only the first length bytes will be remained in the file, and the rest will be deleted. If the length is greater than the file length, the file will be extended, and the extended part will be filled with null bytes ('\\0').
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
Example:
const fs = wx.getFileSystemManager()
// Open a file
fs.open({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
flag: 'a+',
success(res) {
// Truncate the file content
fs.ftruncate({
fd: res.fd,
length: 10, // Truncate the file from the 10th byte
success(res) {
console.log(res)
}
})
}
})

.ftruncateSync

This method is called using undefined FileSystemManager.ftruncateSync(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Synchronously truncates the file content.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
fd
string
-
True
File descriptor obtained through FileSystemManager.open or FileSystemManager.openSync.
length
number
-
True
The truncation position. Default value: 0. If the length is less than the file length (in bytes), only the first length bytes will be remained in the file, and the rest will be deleted. If the length is greater than the file length, the file will be extended, and the extended part will be filled with null bytes ('\\0').
Return value: undefined.
Example:
const fs = wx.getFileSystemManager()
const fd = fs.openSync({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
flag: 'a+'
})
fs.ftruncateSync({
fd: fd,
length: 10 // Truncate the file from the 10th byte
})

.getSavedFileList

This method is called using FileSystemManager.getSavedFileList(Object object).
Feature description:Gets the list of saved local cache files in this mini program.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
object.success callback function parameter:Object res
Property
Type
Description
fileList
Array.<Object>
Array of file objects.
res.fileList structure
Property
Type
Description
filePath
string
Local path.
size
number
Local file size in bytes.
createTime
number
Timestamp when the file was saved, in seconds since 1970/01/01 08:00:00

.mkdir

This method is called using FileSystemManager.mkdir(Object object).
Feature description:Creates a directory.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
dirPath
string
-
True
Path of the directory to create.
recursive
boolean
false
False
Whether to recursively create the parent directories. If the parent directories already exist, they will not be created. For example, if dirPath is a/b/c/d and recursive is true, it will create directory a, then b inside a, and so on until d inside a/b/c.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
object.fail callback function parameter:Object res
Property
Type
Description
errMsg
string
Error message.
Valid values for res.errMsg
Value
Description
fail no such file or directory ${dirPath}
The parent directory does not exist.
fail permission denied, open ${dirPath}
No write permission for the specified filePath.
fail file already exists ${dirPath}
A file or directory with the same name already exists.
Example:
const fs = wx.getFileSystemManager()
fs.mkdir({
dirPath: `${wx.env.USER_DATA_PATH}/example`,
recursive: false,
success(res) {
console.log(res)
},
fail(res) {
console.error(res)
}
})

// Synchronous API
try {
fs.mkdirSync(`${wx.env.USER_DATA_PATH}/example`, false)
} catch(e) {
console.error(e)
}

.mkdirSync

This method is called using FileSystemManager.mkdirSync(string dirPath, boolean recursive).
Feature description:Synchronously performs the same feature as FileSystemManager.mkdir.
Parameter and description:
string dirPath: Path of the directory to create.
boolean recursive: Whether to recursively create all the directory's parent directories before creating the directory. If the parent directories already exist, they will not be created. For example, if dirPath is a/b/c/d and recursive is true, it will create directory a, then b inside a, and so on until d inside a/b/c.
Example:
const fs = wx.getFileSystemManager()
fs.mkdir({
dirPath: `${wx.env.USER_DATA_PATH}/example`,
recursive: false,
success(res) {
console.log(res)
},
fail(res) {
console.error(res)
}
})

// Synchronous API
try {
fs.mkdirSync(`${wx.env.USER_DATA_PATH}/example`, false)
} catch(e) {
console.error(e)
}

.open

This method is called using FileSystemManager.open(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Opens a file and returns a file descriptor.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
filePath
string
-
True
File path (local path).
flag
string
r
False
File system flag. Default value: r.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
Valid values for flag
Value
Description
a
Opens the file for appending. If the file does not exist, it will be created.
ax
Similar to 'a', but the file opening will fail if the path exists.
a+
Opens the file for reading and appending. If the file does not exist, it will be created.
ax+
Similar to 'a+', but the file opening will fail if the path exists.
as
Open file for appending in synchronous mode. If the file does not exist, it will be created.
as+
Open file for reading and appending in synchronous mode. If the file does not exist, it will be created.
r
Opens the file for reading. If the file does not exist, an error will occur.
r+
Opens the file for reading and writing. If the file does not exist, an error will occur.
w
Opens the file for writing. If the file does not exist, create the file; if the file exists, truncate the file.
wx
Similar to 'w', but the file opening will fail if the path exists.
w+
Opens the file for reading and writing. If the file does not exist, create the file; if the file exists, truncate the file.
wx+
Similar to 'w+', but the file opening will fail if the path exists.
object.success callback function parameter:Object res
Property
Type
Description
fd
string
File descriptor.
Example:
const fs = wx.getFileSystemManager()
fs.open({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
flag: 'a+',
success(res) {
console.log(res.fd)
}
})

.openSync

This method is called using string FileSystemManager.openSync(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Synchronously opens the file and returns the file descriptor.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
filePath
string
-
True
File path (local path).
flag
string
r
False
File system flag. Default value: r.
Valid values for flag
Value
Description
a
Opens the file for appending. If the file does not exist, it will be created.
ax
Similar to 'a', but the file opening will fail if the path exists.
a+
Opens the file for reading and appending. If the file does not exist, it will be created.
ax+
Similar to 'a+', but the file opening will fail if the path exists.
as
Open file for appending in synchronous mode. If the file does not exist, it will be created.
as+
Open file for reading and appending in synchronous mode. If the file does not exist, it will be created.
r
Opens the file for reading. If the file does not exist, an error will occur.
r+
Opens the file for reading and writing. If the file does not exist, an error will occur.
w
Opens the file for writing. If the file does not exist, create the file; if the file exists, truncate the file.
wx
Similar to 'w', but the file opening will fail if the path exists.
w+
Opens the file for reading and writing. If the file does not exist, create the file; if the file exists, truncate the file.
wx+
Similar to 'w+', but the file opening will fail if the path exists.
Return value:string, a file descriptor.
Example:
const fs = wx.getFileSystemManager() const fd = fs.openSync({ filePath: `${wx.env.USER_DATA_PATH}/hello.txt`, flag: 'a+' }) console.log(fd)

.read

This method is called using FileSystemManager.read(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Reads a file.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
fd
string
-
True
File descriptor obtained through FileSystemManager.open or FileSystemManager.openSync.
arrayBuffer
ArrayBuffer
-
True
The buffer to which the data is written, and it must be an instance of ArrayBuffer.
offset
number
0
False
The write offset in the buffer. Default value: 0.
length
number
0
False
The number of bytes to be read in the file. Default value: 0.
position
number
-
False
The starting position for reading the file. If this property is not provided or null is entered, the file is read from the position of the current file pointer. If the position is a positive integer, the file pointer position remains unchanged and the file is read from the position.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
object.success callback function parameter:Object res
Property
Type
Description
bytesRead
number
Number of bytes actually read.
arrayBuffer
ArrayBuffer
The buffer object that was written to, same as the arrayBuffer parameter passed to the API.
Example:
const fs = wx.getFileSystemManager()
const ab = new ArrayBuffer(1024)
// Open a file
fs.open({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
flag: 'a+',
success(res) {
// Read the file into an ArrayBuffer
fs.read({
fd: res.fd,
arrayBuffer: ab,
length: 10,
success(res) {
console.log(res)
}
})
}
})

.readSync

This method is called using ReadResult FileSystemManager.readSync(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Reads a file.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
fd
string
-
True
File descriptor obtained through FileSystemManager.open or FileSystemManager.openSync.
arrayBuffer
ArrayBuffer
-
True
The buffer to which the data is written, and it must be an instance of ArrayBuffer.
offset
number
0
False
The write offset in the buffer. Default value: 0.
length
number
0
False
The number of bytes to be read in the file. Default value: 0.
position
number
-
False
The starting position for reading the file. If this property is not provided or null is entered, the file is read from the position of the current file pointer. If the position is a positive integer, the file pointer position remains unchanged and the file is read from the position.
Return value:ReadResult. The result of reading the files.
Example:
const fs = wx.getFileSystemManager()
const ab = new ArrayBuffer(1024)
const fd = fs.openSync({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
flag: 'a+'
})
const res = fs.readSync({
fd: fd,
arrayBuffer: ab,
length: 10
})
console.log(res)

.readCompressedFile

This method is called using FileSystemManager.readCompressedFile(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Reads the content of a local file with a specified compression type.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
filePath
string
-
True
Path of the file to read (local user file or code package file).
compressionAlgorithm
string
-
True
File compression type, currently only supports br.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
Valid values for object.compressionAlgorithm:
Value
Description
br
Brotli-compressed file.
object.success callback function parameter:Object res
Property
Type
Description
data
ArrayBuffer
File content.
Example:
const fs = wx.getFileSystemManager()

// Asynchronous API
fs.readCompressedFile({
filePath: '${wx.env.USER_DATA_PATH}/hello.br',
compressionAlgorithm: 'br',
success(res) {
console.log(res.data)
},
fail(res) {
console.log('readCompressedFile fail', res)
}
})

// Synchronous API
const data = fs.readCompressedFileSync({
filePath: '${wx.env.USER_DATA_PATH}/hello.br',
compressionAlgorithm: 'br',
})
console.log(data)

.readCompressedFileSync

This method is called using ArrayBuffer FileSystemManager.readCompressedFileSync(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Synchronously reads the local file contents of specified compression type.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
filePath
string
-
True
Path of the file to read (local user file or code package file).
compressionAlgorithm
string
-
True
File compression type, currently only supports br.
Valid values for object.compressionAlgorithm:
Value
Description
br
Brotli-compressed file.
Return value:ArrayBuffer, the result of reading the files.
Example:
const fs = wx.getFileSystemManager()

// Asynchronous API
fs.readCompressedFile({
filePath: '${wx.env.USER_DATA_PATH}/hello.br',
compressionAlgorithm: 'br',
success(res) {
console.log(res.data)
},
fail(res) {
console.log('readCompressedFile fail', res)
}
})

// Synchronous API
try {
const data = fs.readCompressedFileSync({
filePath: '${wx.env.USER_DATA_PATH}/hello.br',
compressionAlgorithm: 'br',
})
console.log(data)
} catch (err) {
console.log(err)
}

.readdir

This method is called using FileSystemManager.readdir(Object object).
Feature description:Reads the list of files in the directory.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
dirPath
string
-
True
The path to the directory to be read.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
object.success callback function parameter:Object res
Property
Type
Description
files
Array.<string>
An array of filenames in a specified directory.
object.fail callback function parameter:Object res
Property
Type
Description
errMsg
string
Error message.
Valid values for res.errMsg
Value
Description
fail no such file or directory ${dirPath}
The directory does not exist.
fail not a directory ${dirPath}
The dirPath is not a directory.
fail permission denied, open ${dirPath}
No read permission for the specified filePath.
Example:
Note:
The readdir API cannot access the root path of the file system (wxfile://).
const fs = wx.getFileSystemManager()
fs.readdir({
dirPath: `${wx.env.USER_DATA_PATH}/example`,
success(res) {
console.log(res.files)
},
fail(res) {
console.error(res)
}
})

// Synchronous API
try {
const res = fs.readdirSync(`${wx.env.USER_DATA_PATH}/example`)
console.log(res)
} catch(e) {
console.error(e)
}

.readdirSync

This method is called using Array.<string>FileSystemManager.readdirSync(string dirPath).
Feature description:Synchronously performs the same feature as FileSystemManager.readdir.
Parameter and description:string dirPath, the path to the directory to be read.
Return value:Array.<string> files, an array of file names in a specified directory.
Example:
const fs = wx.getFileSystemManager()
fs.readdir({
dirPath: `${wx.env.USER_DATA_PATH}/example`,
success(res) {
console.log(res.files)
},
fail(res) {
console.error(res)
}
})

// Synchronous API
try {
const res = fs.readdirSync(`${wx.env.USER_DATA_PATH}/example`)
console.log(res)
} catch(e) {
console.error(e)
}

.readFile

This method is called using FileSystemManager.readFile(Object object).
Feature description:Reads the content of a local file. The maximum size of a single file is 100 MB.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
filePath
string
-
True
The path to the file to be read.
encoding
string
-
False
Specify the file's character encoding for reading. If no encoding is provided, the file's binary content will be read as an ArrayBuffer.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
Valid values for object.encoding
Value
Description
ascii
-
base64
-
binary
-
hex
-
ucs2/ucs-2/utf16le/utf-16le
Reads in little-endian order.
utf-8/utf8
-
latin1
-
object.success callback function parameter:Object res
Property
Type
Description
data
string/ArrayBuffer
File content.
object.fail callback function parameter:Object res
Property
Type
Description
errMsg
string
Error message.
Valid values for res.errMsg
Value
Description
fail no such file or directory, open ${filePath}
The specified filePath directory does not exist.
fail permission denied, open ${dirPath}
No read permission for the specified filePath.
Example:
const fs = wx.getFileSystemManager()
fs.readFile({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
encoding: 'utf8',
position: 0,
success(res) {
console.log(res.data)
},
fail(res) {
console.error(res)
}
})

// Synchronous API
try {
const res = fs.readFileSync(`${wx.env.USER_DATA_PATH}/hello.txt`, 'utf8', 0)
console.log(res)
} catch(e) {
console.error(e)
}

.readFileSync

This method is called using string|ArrayBuffer FileSystemManager.readFileSync(string filePath, string encoding).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Synchronously performs the same feature as FileSystemManager.readFile.
Parameter and description:
string filePath: The path to the file to be read.
string encoding: Specifies the file's character encoding for reading. If no encoding is provided, the file's binary content will be read as an ArrayBuffer.
Valid values for encoding
Value
Description
ascii
-
base64
-
binary
-
hex
-
ucs2/ucs-2/utf16le/utf-16le
Reads in little-endian order.
utf-8/utf8
-
latin1
-
Return value:string|ArrayBuffer data, file content.
Example:
const fs = wx.getFileSystemManager()
fs.readFile({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
encoding: 'utf8',
position: 0,
success(res) {
console.log(res.data)
},
fail(res) {
console.error(res)
}
})

// Synchronous API
try {
const res = fs.readFileSync(`${wx.env.USER_DATA_PATH}/hello.txt`, 'utf8', 0)
console.log(res)
} catch(e) {
console.error(e)
}

.readZipEntry

This method is called using FileSystemManager.readZipEntry(Object object).
Feature description:Read files in a compressed package.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
filePath
string
-
True
The path of the zip file to be read (local path).
encoding
string
-
False
Specifies the character encoding for reading files. This is only effective when the entries value is "all". If entries is "all" and no encoding is provided, the file's binary content will be read as an ArrayBuffer.
entries
Array.<Object>/'all'
-
True
The list of files within the zip archive to be read (passing "all" means reading all files in the archive).
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).

Valid values for encoding

Value
Description
ascii
-
base64
-
binary
-
hex
-
ucs2/ucs-2/utf16le/utf-16le
Reads in little-endian order.
utf-8/utf8
-
latin1
-
entries structural property
Structural property
Type
Default value
‍Required
Description
path
string
-
True
The path to the files in the compressed package.
encoding
string
-
False
Specifies the file's character encoding for reading. If no encoding is provided, the file's binary content will be read as an ArrayBuffer. The valid values are the same as those for encoding.
position
number
-
False
The position to start reading from within the file. If not specified, reading starts from the beginning. The range should be [position, position+length). Valid range: [0, fileLength - 1]. Unit: byte.
length
number
-
False
The length of the file to read. If not specified, reads until the end of the file. Valid range: [1, fileLength]. Unit: byte.
object.success callback function parameter:Object res
Property
Type
Description
entries
Object
The result of reading the files. res.entries is an object where the key is the file path and the value is a FileItem object representing the read result. Each FileItem contains data (file content) and errMsg (error message) properties.
entries structural property
Structural property
Type
Description
path
string
File path.
path structural property
Structural property
Type
Description
data
string/ArrayBuffer
File content.
errMsg
string
Error message.
Example:
const fs = wx.getFileSystemManager()
// Read one or more files in the zip package
fs.readZipEntry({
filePath: 'wxfile://from/to.zip',
entries: [{
path: 'some_folder/my_file.txt', // File path within the zip package
encoding: 'utf-8', // Specify the file's character encoding for reading. If no encoding is provided, the file's binary content will be read as an ArrayBuffer.
position: 0, // The position to start reading from within the file. If not specified, reading starts from the beginning. The range should be [position, position+length). Valid range: [0, fileLength - 1]. Unit: byte.
length: 10000, // The length of the file to read. If not specified, reads until the end of the file Valid range: [1, fileLength]. Unit: byte.
}, {
path: 'other_folder/orther_file.txt', // The path to the files within the zip package
}],
success(res) {
console.log(res.entries)
// res.entries === {
// 'some_folder/my_file.txt': {
// errMsg: 'readZipEntry:ok',
// data: 'xxxxxx'
// },
// 'other_folder/orther_file.txt': {
// data: (ArrayBuffer)
// }
// }
},
fail(res) {
console.log(res.errMsg)
},
})

// Read all files in the zip package Allow specifying a unified encoding. position and length are not allowed to be specified, defaulting to 0 and the file length respectively
fs.readZipEntry({
filePath: 'wxfile://from/to.zip',
entries: 'all'
encoding: 'utf-8', // Specify the file's character encoding for reading. If no encoding is provided, the file's binary content will be read as an ArrayBuffer
success(res) {
console.log(res.entries)
// res.entries === {
// 'some_folder/my_file.txt': {
// errMsg: 'readZipEntry:ok',
// data: 'xxxxxx'
// },
// 'other_folder/orther_file.txt': {
// errMsg: 'readZipEntry:ok',
// data: 'xxxxxx'
// }
// }
},
fail(res) {
console.log(res.errMsg)
},
})

.removeSavedFile

This method is called using FileSystemManager.removeSavedFile(Object object).
Feature description:Deletes saved local cache files in this mini program.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
filePath
string
-
True
The path to the file that needs to be deleted.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
object.fail callback function parameter:Object res
Property
Type
Description
errMsg
string
Error message.
Valid values for res.errMsg
Value
Description
fail file not exist
The file cannot be found through the specified tempFilePath.

.rename

This method is called using FileSystemManager.rename(Object object).
Feature description:Renames a file. The files can be moved from oldPath to newPath.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
oldPath
string
-
True
The source file path, which can be a regular file or directory, supporting local paths.
newPath
string
-
True
The new file path, supporting local paths.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
object.fail callback function parameter:Object res
Property
Type
Description
errMsg
string
Error message.
Valid values for res.errMsg
Value
Description
fail permission denied, rename ${oldPath} -> ${newPath}
The specified source or target file does not have write permissions.
fail no such file or directory, rename ${oldPath} -> ${newPath}
Source file does not exist, or the parent directory of the destination file path does not exist.
Example:
const fs = wx.getFileSystemManager()
fs.rename({
oldPath: `${wx.env.USER_DATA_PATH}/hello.txt`,
newPath: `${wx.env.USER_DATA_PATH}/hello_new.txt`,
success(res) {
console.log(res)
},
fail(res) {
console.error(res)
}
})

// Synchronous API
try {
const res = fs.renameSync(
`${wx.env.USER_DATA_PATH}/hello.txt`,
`${wx.env.USER_DATA_PATH}/hello_new.txt`
)
console.log(res)
} catch(e) {
console.error(e)
}

.renameSync

This method is called using FileSystemManager.renameSync(string oldPath, string newPath).
Feature description:Synchronously performs the same feature as FileSystemManager.rename.
Parameter and description
string oldPath: The source file path, which can be a regular file or directory, supporting local paths.
string newPath: The new file path, supporting local paths.
Example:
const fs = wx.getFileSystemManager()
fs.rename({
oldPath: `${wx.env.USER_DATA_PATH}/hello.txt`,
newPath: `${wx.env.USER_DATA_PATH}/hello_new.txt`,
success(res) {
console.log(res)
},
fail(res) {
console.error(res)
}
})

// Synchronous API
try {
const res = fs.renameSync(
`${wx.env.USER_DATA_PATH}/hello.txt`,
`${wx.env.USER_DATA_PATH}/hello_new.txt`
)
console.log(res)
} catch(e) {
console.error(e)
}

.rmdir

This method is called using FileSystemManager.rmdir(Object object).
Feature description:Deletes a directory.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
dirPath
string
-
True
The path of the directory to be deleted.
recursive
boolean
false
False
Whether to recursively delete the directory. If true, deletes the directory and all its subdirectories and files.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
object.fail callback function parameter:Object res
Property
Type
Description
errMsg
string
Error message.
Valid values for res.errMsg
Value
Description
fail no such file or directory ${dirPath}
The directory does not exist.
fail directory not empty
The directory is not empty.
fail permission denied, open ${dirPath}
The specified dirPath does not have write permissions.
Example:
const fs = wx.getFileSystemManager()
fs.rmdir({
dirPath: `${wx.env.USER_DATA_PATH}/example`,
recursive: false,
success(res) {
console.log(res)
},
fail(res) {
console.error(res)
}
})

// Synchronous API
try {
const res = fs.rmdirSync(`${wx.env.USER_DATA_PATH}/example`, false)
console.log(res)
} catch(e) {
console.error(e)
}

.rmdirSync

This method is called using FileSystemManager.rmdirSync(string dirPath, boolean recursive).
Feature description:Synchronously performs the same feature as FileSystemManager.rmdir.
Parameter and description:
string dirPath: The path of the directory to be deleted.
boolean recursive: Whether to recursively delete the directory. If true, deletes the directory and all its subdirectories and files.
Example:
const fs = wx.getFileSystemManager()
fs.rmdir({
dirPath: `${wx.env.USER_DATA_PATH}/example`,
recursive: false,
success(res) {
console.log(res)
},
fail(res) {
console.error(res)
}
})

// Synchronous API
try {
const res = fs.rmdirSync(`${wx.env.USER_DATA_PATH}/example`, false)
console.log(res)
} catch(e) {
console.error(e)
}

.saveFile

This method is called using FileSystemManager.saveFile(Object object).
Feature description:Saves a temporary file to a local path. This API will move the temporary file, so after a successful call, tempFilePath will no longer be available.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
tempFilePath
string
-
True
The path of the temporary file.
filePath
string
-
False
The path where the file will be saved.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
object.success callback function parameter:Object res
Property
Type
Description
savedFilePath
number
The path of the saved file.
object.fail callback function parameter:Object res
Property
Type
Description
errMsg
string
Error message.
Valid values for res.errMsg
Value
Description
fail tempFilePath file not exist
The specified tempFilePath could not be found.
fail permission denied, open "${filePath}"
No write permission for the specified filePath.
fail no such file or directory "${dirPath}"
The parent directory does not exist.

.saveFileSync

This method is called using number FileSystemManager.saveFileSync(string tempFilePath, string filePath).
Feature description:Synchronously performs the same feature as FileSystemManager.saveFile.
Parameter and description:
string tempFilePath: The path of the temporary file.
string filePath: The path to the file to be saved.
Return value:number savedFilePath. The path where the file will be saved.

.stat

This method is called using FileSystemManager.stat(Object object).
Feature description:Gets the Stats object of a file.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
path
string
-
True
The path of the file or directory.
recursive
boolean
false
False
Whether to recursively get the Stats information of each file in the directory.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
object.success callback function parameter:Object res
Property
Type
Description
stats
Stats/Object
When recursive is false, res.stats is a Stats object. When recursive is true and path is a directory, res.stats is an Object where the key is the relative path from the root path and the value is the corresponding Stats object.
object.fail callback function parameter:Object res
Property
Type
Description
errMsg
string
Error message.
Valid values for res.errMsg
Value
Description
fail permission denied, open ${path}
The specified path does not have read permissions.
fail no such file or directory ${path}
The file does not exist.
Example:
When recursive is false:
// Asynchronous version
let fs = wx.getFileSystemManager()
fs.stat({
path: `${wx.env.USER_DATA_PATH}/testDir`,
success: res => {
console.log(res.stats.isDirectory())
}
})
// Synchronous version
fs.statSync(`${wx.env.USER_DATA_PATH}/testDir`, false)
When recursive is true:
let fs = wx.getFileSystemManager()
// Asynchronous version
fs.stat({
path: `${wx.env.USER_DATA_PATH}/testDir`,
recursive: true,
success: res => {
Object.keys(res.stats).forEach(path => {
let stats = res.stats[path]
console.log(path, stats.isDirectory())
})
}
})
// Synchronous version
fs.statSync(`${wx.env.USER_DATA_PATH}/testDir`, true)

.statSync

This method is called using Stats|Object FileSystemManager.statSync(string path, boolean recursive).
Feature description:Synchronously performs the same feature as FileSystemManager.stat.
Parameter and description:
string path: The path to the file/directory.
boolean recursive: Whether to recursively get the stats of each file in the directory.
Return value:Stats|Object stats. When recursive is false, res.stats is a Stats object. When recursive is true and path is a directory, res.stats is an Object where the key is the relative path from the root path and the value is the corresponding Stats object.
Example:
When recursive is false:
// Asynchronous version
let fs = wx.getFileSystemManager()
fs.stat({
path: `${wx.env.USER_DATA_PATH}/testDir`,
success: res => {
console.log(res.stats.isDirectory())
}
})
// Synchronous version
fs.statSync(`${wx.env.USER_DATA_PATH}/testDir`, false)
When recursive is true:
let fs = wx.getFileSystemManager()
// Asynchronous version
fs.stat({
path: `${wx.env.USER_DATA_PATH}/testDir`,
recursive: true,
success: res => {
Object.keys(res.stats).forEach(path => {
let stats = res.stats[path]
console.log(path, stats.isDirectory())
})
}
})
// Synchronous version
fs.statSync(`${wx.env.USER_DATA_PATH}/testDir`, true)

.truncate

This method is called using FileSystemManager.truncate(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Truncates the file content.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
filePath
string
-
True
Path to the file to be truncated (local path).
length
number
0
False
The truncation position. Default value: 0. If the length is less than the file length (in bytes), only the first length bytes will be remained in the file, and the rest will be deleted. If the length is greater than the file length, the file will be expanded with empty bytes ('\\0').
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
Example:
const fs = wx.getFileSystemManager()
fs.truncate({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
length: 10, // Truncate from the 10th byte
success(res) {
console.log(res)
}
})

.truncateSync

This method is called using undefined FileSystemManager.truncateSync(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Synchronously truncates file contents.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
filePath
string

True
Path to the file to be truncated (local path).
length
number
0
False
The truncation position. Default value: 0. If the length is less than the file length (in bytes), only the first length bytes will be remained in the file, and the rest will be deleted. If the length is greater than the file length, the file will be expanded with empty bytes ('\\0').
Return value:undefined.
Example:
const fs = wx.getFileSystemManager()
fs.truncateSync({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
length: 10, // Truncate from the 10th byte
})
This method is called using FileSystemManager.unlink(Object object).
Feature description:Deletes a file.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
filePath
string
-
True
Path to the file to be deleted.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
object.fail callback function parameter:Object res
Property
Type
Description
errMsg
string
Error message.
Valid values for res.errMsg
Value
Description
fail permission denied, open ${path}
The specified path does not have read permissions.
fail no such file or directory ${path}
The file does not exist.
fail operation not permitted, unlink ${filePath}
The provided filePath is a directory.
Example:
const fs = wx.getFileSystemManager()
fs.unlink({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
success(res) {
console.log(res)
},
fail(res) {
console.error(res)
}
})

// Synchronous API
try {
const res = fs.unlinkSync(`${wx.env.USER_DATA_PATH}/hello.txt`)
console.log(res)
} catch(e) {
console.error(e)
}

.unlinkSync

This method is called using FileSystemManager.unlinkSync(string filePath).
Feature description:Synchronously performs the same feature as FileSystemManager.unlink .
Parameter and description:string filePath: Path to the file to be deleted.
Example:
const fs = wx.getFileSystemManager()
fs.unlink({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
success(res) {
console.log(res)
},
fail(res) {
console.error(res)
}
})

// Synchronous API
try {
const res = fs.unlinkSync(`${wx.env.USER_DATA_PATH}/hello.txt`)
console.log(res)
} catch(e) {
console.error(e)
}

.unzip

This method is called using FileSystemManager.unzip(Object object).
Feature description:Decompresses a file.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
zipFilePath
string
-
True
Source file path, must be a zip file.
targetPath
string
-
True
The path to the destination directory.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
object.fail callback function parameter:Object res
Property
Type
Description
errMsg
string
Error message.
Valid values for res.errMsg
Value
Description
fail permission denied, unzip ${zipFilePath} -> ${destPath}
No write permission for the destination file path.
fail no such file or directory, unzip ${zipFilePath} -> ${destPath}
Source file does not exist, or the parent directory of the destination file path does not exist.
Example:
const fs = wx.getFileSystemManager()
fs.unzip({
zipFilePath: `${wx.env.USER_DATA_PATH}/example.zip`,
targetPath: '${wx.env.USER_DATA_PATH}/example',
success(res) {
console.log(res)
},
fail(res) {
console.error(res)
}
})

.write

This method is called using FileSystemManager.write(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Writes to a file.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
fd
string
-
True
File descriptor obtained through FileSystemManager.open or FileSystemManager.openSync.
data
string/ArrayBuffer
-
True
The text or binary data to be written.
offset
number
0
False
Effective only when data is of type ArrayBuffer. Specifies the index in the ArrayBuffer to start writing from. Default is 0.
length
number
-
False
Effective only when data is of type ArrayBuffer. Specifies the number of bytes to write. Default is the remaining bytes from offset.
encoding.
string
utf8
False
Character encoding for writing to the file.
position
number
-
False
Specifies the offset from the beginning of the file where data should be written. If not provided or not a number, data will be written at the current pointer position.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
Valid values for encoding
Value
Description
ascii
-
base64
-
binary
-
hex
-
ucs2/ucs-2/utf16le/utf-16le
Reads in little-endian order.
utf-8/utf8
-
latin1
-
object.success callback function parameter: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 match the number of characters in the string).
Example:
const fs = wx.getFileSystemManager()
// Open a file
fs.open({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
flag: 'a+',
success(res) {
// Write to the file
fs.write({
fd: res.fd,
data: 'some text',
success(res) {
console.log(res.bytesWritten)
}
})
}
})

.writeSync

This method is called using FileSystemManager.writeFile(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Synchronously writes to a file.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
fd
string
-
True
File descriptor obtained through FileSystemManager.open or FileSystemManager.openSync.
data
string/ArrayBuffer
-
True
The text or binary data to be written.
offset
number
0
False
Effective only when data is of type ArrayBuffer. Specifies the index in the ArrayBuffer to start writing from. Default is 0.
length
number
-
False
Effective only when data is of type ArrayBuffer. Specifies the number of bytes to write. Default is the remaining bytes from offset.
encoding
string
utf8
False
Character encoding for writing to the file.
position
number
-
False
Specifies the offset from the beginning of the file where data should be written. If not provided or not a number, data will be written at the current pointer position.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
Valid values for encoding
Value
Description
ascii
-
base64
-
binary
-
hex
-
ucs2/ucs-2/utf16le/utf-16le
Reads in little-endian order.
utf-8/utf8
-
latin1
-
object.success callback function parameter: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 match the number of characters in the string).
Example:
const fs = wx.getFileSystemManager()
const fd = fs.openSync({
filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
flag: 'a+'
})
const res = fs.writeSync({
fd: fd,
data: 'some text'
})
console.log(res.bytesWritten)

.writeFile

This method is called using FileSystemManager.writeFile(Object object).
Feature description:Writes a file.
Parameter and description:Object object
Property
Type
Default value
‍Required
Description
filePath
string
-
True
The path to the file to be written.
data
string/ArrayBuffer
-
True
The text or binary data to be written.
encoding.
string
utf8
False
Character encoding for writing to the file.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
Valid values for object.encoding
Value
Description
ascii
-
base64
-
binary
-
hex
-
ucs2/ucs-2/utf16le/utf-16le
Reads in little-endian order.
utf-8/utf8
-
latin1
-
object.fail callback function parameter:Object res
Property
Type
Description
errMsg
string
Error message.
Valid values for res.errMsg
Value
Description
fail no such file or directory, open ${filePath}
The specified filePath directory does not exist.
fail permission denied, open ${dirPath}
No write permission for the specified filePath.
Example:
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 API
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)
}

.writeFileSync

This method is called using FileSystemManager.writeFileSync(string filePath, string|ArrayBuffer data, string encoding).
Feature description:Synchronously performs the same feature as FileSystemManager.writeFile.
Parameter and description:
string filePath: The path to the file to be written.
string|ArrayBuffer data: The text or binary data to be written.
string encoding: The character encoding for writing to the file.
Valid values for encoding
Value
Description
ascii
-
base64(When using base64 encoding, only pass 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/ucs-2/utf16le/utf-16le
Reads in little-endian order.
utf-8/utf8
-
latin1
-
Example:
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 API
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)
}

Error codes

Note:
Unless otherwise specified, the error codes are subject to the following table.
Error code
Error message.
Description
1300001
operation not permitted
The operation is not allowed (e.g., it is expected to provide a file for filePath but actually a directory is provided).
1300002
no such file or directory ${path}
File/directory does not exist, or the parent directory is missing.
1300005
Input/output error
Input/output stream is unavailable.
1300009
bad file descriptor
Invalid file descriptor.
1300013
permission denied
Permission error. The file is read-only or write-only.
1300014
Path permission denied
No permission for the specified path.
1300020
not a directory
The specified path is not a directory.
1300021
Is a directory
The specified path is a directory.
1300022
Invalid argument
Invalid argument, check if length or offset is out of range.
1300036
File name too long
The filename is too long.
1300066
directory not empty
The directory is not empty.
1300201
system error
The system API is failed to be called.
1300202
the maximum size of the file storage limit is exceeded
Storage space is insufficient, or file size exceeds the limit (100 MB).
1300203
base64 encode error
Character encoding conversion failed (e.g., incorrect base64 format).
1300300
sdcard not mounted
Failed to mount the Android SDCard.
1300301
unable to open as fileType
Unable to open the file as the specified fileType.
1301000
permission denied, cannot access file path
No access to the target path (e.g., usr directory)
1301002
data to write is empty
The data to be written is empty.
1301003
illegal operation on a directory
Illegal operation on a directory (e.g., specified filePath is an existing directory).
1301004
illegal operation on a package directory
Illegal operation on a code 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 length value is out of range.
1301007
value of offset is out of range
The offset value is out of range.
1301009
value of position is out of range
The position value is out of range.
1301100
store directory is empty
The store directory is empty.
1301102
unzip open file fail
Failed to open the compressed file.
1301103
unzip entry fail
Failed to unzip a single file.
1301104
unzip fail
Unzipping failed.
1301111
brotli decompress fail
Brotli decompression failed (e.g., the specified compressionAlgorithm does not match the actual compression format of the file).
1301112
tempFilePath file not exist
The specified tempFilePath could not be found.
1302001
fail permission denied
No read/write permission for the specified fd path.
1302002
exceed max concurrent fd limit
Maximum number of file descriptors reached.
1302003
invalid flag
Invalid flag.
1302004
permission denied when open using flag
Unable to open the file using the specified flag.
1302005
array buffer does not exist
arrayBuffer is not provided.
1302100
array buffer is readonly
The arrayBuffer is read-only.

Stats

An object that describes the status of a file.

Property

number mode: The type and access permissions of the file, corresponding to POSIX stat.st_mode.
number size: The file size in bytes, corresponding to POSIX stat.st_size.
number lastAccessedTime: The last time the file was accessed or executed, in UNIX timestamp format, corresponding to POSIX.
number lastModifiedTime: The last time the file was modified, in UNIX timestamp format, corresponding to POSIX stat.st_mtime.

Methods

.isDirectory

This method is called using boolean Stats.isDirectory().
Feature description:Checks whether the current file is a directory.
Return value:boolean, indicating whether the current file is a directory.

.isFile

This method is called using boolean Stats.isFile().
Feature description:Checks whether the current file is a normal file.
Return value:boolean, indicating whether the current file is a normal file.

ReadResult

Feature description:The result of reading the files. It is returned using FileSystemManager.readSync.
Property:
number bytesRead: The actual number of bytes read.
ArrayBuffer arrayBuffer: The buffer object into which the data was read, which is the same as the arrayBuffer passed as an input parameter to the API.

WriteResult

Feature description:The result of writing to a file. It is returned using FileSystemManager.writeSync.
Property:number bytesWritten: The actual number of bytes written to the file (note that the number of bytes written may not match the number of characters in the string).


Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 available.

7x24 Phone Support