The file system in mini games provides a storage solution isolated by mini game and user dimensions, along with a set of management APIs. You can learn the user’s subscription status for related template messages using getFileSystemManager. All file system management operations are performed through the FileSystemManager. var fs = wx.getFileSystemManager()
Files are mainly divided into two types:
Code package files: These are files added to the project directory.
Local files: These are files generated locally through API calls or downloaded from the network and stored locally.
Local files are further divided into three types:
Local temporary files: These are temporary files that can be reclaimed at any time. There is no storage size limit.
Local cache files: These are files generated by caching local temporary files through APIs. The directory and file names cannot be customized. Together with local user files, a mini program (including mini games) can store up to 200 MB.
Local user files: These are files generated by caching local temporary files through APIs, allowing custom directories and file names. Together with local cache files, a mini program (including mini games) can store up to 200 MB.
Code package files
Due to the size limitation of code package files, they are suitable for files needed during the initial load. For larger files or files that need to be dynamically replaced, it is recommended to download them locally after the mini game starts.
Access to code package files
Code package files are accessed by writing the file path starting from the project root directory. Relative paths are not supported. For example,/a/b/c
and a/b/c
are valid, while./a/b/c
and ../a/b/c
are not.
Modify code package files
Files within the code package cannot be dynamically modified or deleted after running. Modifying code package files requires republishing the version.
Local files
Local files refer to a separate file storage area created when a mini game is added to a user's phone, isolated by user dimension. This means that on the same phone, each user cannot access files of other logged-in users, and files between different appIds for the same user cannot be accessed.
The file paths for local files are in the following format:
{{protocol name}}://file path
Note:
The protocol is "wxfile" on iOS/Android clients and "http" in the developer tools. Developers do not need to worry about this difference and should not hardcode the complete file path in the code.
Local temporary files
Local temporary files can only be generated by calling specific APIs and cannot be directly written. They are only guaranteed to be valid during the current lifecycle and may not be available after a restart. Therefore,do not store local temporary file paths for future use.If you need to use them later, convert them to local cache files or local user files using FileSystemManager.saveFile or FileSystemManager.copyFile.
Example:
wx.chooseImage({
success: function (res) {
var tempFilePaths = res.tempFilePaths
}
})
Local cache files
Local cache files can only be generated by calling specific APIs and cannot be directly written. They remain available after a restart. Local cache files can only be obtained by saving local temporary files using the FileSystemManager.saveFile.
Example:
fs.saveFile({
tempFilePath: '',
success(res) {
console.log(res.savedFilePath)
}
})
Local user files
A user file directory is provided to developers, where they have full read and write permissions. The path to this directory can be obtained using wx.env.USER_DATA_PATH
.
Example:
const fs = wx.getFileSystemManager()
fs.writeFileSync(`${wx.env.USER_DATA_PATH}/hello.txt`, 'hello, world', 'utf8')
Read & write permissions
|
Code package files | Yes | No |
Local temporary documents | Yes | No |
Local cache files | Yes | No |
Local user files | Yes | Yes |
Cleanup strategy
Local temporary files: These files are only guaranteed to be available during the current lifecycle of the mini game. Once the mini game is closed, these files may be cleaned up, meaning they may not be available the next time the game is started.
Local cache files and local user files: The cleanup timing for these files is the same as for code package files. They will only be cleaned up when the code package itself is cleaned up.