File System
The file system is a set of storage and corresponding management interfaces provided by the mini-game, which is isolated from the mini-game and user dimensions. The globally unique file system manager can be obtained throughwx.getFileSystemManager(), and all file system management operations are called throughFileSystemManagerTo invoke.
var fs = wx.getFileSystemManager();Files are mainly divided into two categories:
- Code package file: Code package file refers to the file added in the project directory.
- Local file: A file generated locally by calling an interface, or downloaded from the network and stored locally
Local files are divided into three types:
- Local temporary files: files that are temporarily generated and can be recycled at any time. A maximum of 4GB can be stored during operation. After the operation ends, if more than 2GB has been used, the file will be cleaned up from the most recent use time to less than 2GB.
- Local cache file: The file generated after the mini program caches the local temporary file through the interface. The directory and file name cannot be customized. It is totaled with the local user file. The mini program (including mini games) can store up to 200MB.
- Local user file: The file generated after the mini program caches the local temporary file through the interface. The directory and file name can be customized. It is totaled with the local cache file. The mini program (including mini games) can store up to 200MB.
Code package file
Due to the size limit of the code package file, the code package file is suitable for placing files required for the first load. For files with large content or that need to be replaced dynamically, they are not pushed to the code package. It is recommended to download them to the local using the download interface after the mini program is started.
Access code package files
The access method of code package files is to write the file path starting from the project root directory. Relative path writing is not supported, such as: /a/b/c, a/b/c are legal, ./a/b/c, ../a/b/c are illegal.
Modify code package files
The files in the code package cannot be dynamically modified or deleted after running. Modifying the code package file requires re-releasing the version.
Local files
Local files refer to a separate file storage area after the mini program is added to the mobile phone by the user. It is isolated in the user dimension, that is, on the same mobile phone, each user cannot access the files of other logged-in users, and the files between different appIds of the same user cannot access each other.
The file path of local files is in the following format:
{{protocol name}}://file pathTIP
Among them, the protocol name is 'wxfile' on the iOS/Android client and 'http' on the developer tool. Developers do not need to pay attention to this difference and should not hard-code the complete file path in the code.
Local temporary files
Local temporary files can only be generated by calling specific interfaces, and cannot be written directly. After the local temporary files are generated, they are only guaranteed to be valid within the current life cycle, and may not be available after restart. If you need to ensure that there is no need to download the next time you start, you can convert the local temporary files into local cache files or local user files through theFileSystemManager.saveFile() orFileSystemManager.copyFile()interfaces.
Code example:
wx.chooseImage({
success: function (res) {
var tempFilePaths = res.tempFilePaths; // each entry in tempFilePaths is a local temp file path
},
});local cache file
Local cache files can only be generated by calling a specific interface and cannot directly write content. After a local cache file is generated, it will still be available after restarting. Local cache files can only be generated throughFileSystemManager.saveFile()The interface is obtained by saving local temporary files.
Code example:
fs.saveFile({
tempFilePath: "", // Pass in a local temporary file path
success(res) {
console.log(res.savedFilePath); // res.savedFilePath is a local cache file path
},
});local user profile
We provide a user profile directory to developers. Developers have completely free read and write permissions on this directory. The path to this directory can be obtained through wx.env. USER_DATA_PATH.
Code example:
// Create a file hello.txt in the local user file directory and write "hello, world" to it
const fs = wx.getFileSystemManager();
fs.writeFileSync(`${wx.env.USER_DATA_PATH}/hello.txt`, "hello, world", "utf8");Read and write permissions
| interface, components | read | write |
|---|---|---|
| Code package file | have | none |
| local temporary file | have | none |
| local cache file | have | none |
| local user profile | have | have |
Cleanup strategy
- The local temporary file is only guaranteed to be within the current life cycle of the mini program. Once the mini program is closed, it may be cleared, that is, it is not guaranteed to be available the next time it is cold restarted.
- The cleaning timing of local cache files and local user files is the same as that of code packages. They will only be cleaned together when the code package is cleared.