# File system
File system is a set of Mini Program and user dimension isolated storage and a set of corresponding management interface. adopt wx.getFileSystemManager() Can get to the globally unique file system manager, all file system management operations through the FileSystemManager To invoke...
var fs = wx.getFileSystemManager()
Documents fall into two main categories:
- Code package files: Code package files refer to files that are added to the project directory.
- Local File: A file that is generated locally by calling the interface, or downloaded over the network, and stored locally.
Local files are divided into three types:
- Local Temporary Files: Files that are generated temporarily and are subject to collection at any time. Maximum storage at runtime 4 GB, after the end of the run, if you have used more than 2 GB, will clean up at least 2 GB from far to near according to the most recent usage time.
- Local cache file: Mini Program through the interface to the local temporary file cache generated by the file, can not customize the directory and file name. With a total of local user files, Mini programs (including small games) can be stored up to 200MB。
- Local User File: A file generated by an Mini Program that caches local temporary files through an interface, allowing custom directories and filenames. With a total of local cache files, Mini programs (including small games) can be stored up to 200MB。
# Code package file
Because of the code pack file size limitation, the code pack files are suitable for placing files that are needed for the first time loading. For files that are larger or need to be dynamically replaced, it is not recommended to add to the code pack, and it is recommended to download locally using the download interface after the game starts.
# Access Code Package File
The code package file is accessed from the project root directory by writing to the file path. Relative paths are not supported. Such as:/a/b/c
、a/b/c
All legal,./a/b/c
../a/b/c
Is illegal.
# Modify Code Package File
The files in the code package cannot be dynamically modified or deleted after running. Modifying the code package files requires redistributing the version.
# Local file
Local file refers to the Mini Program is added to the phone by the user, there will be a separate file storage area, isolated by the user dimension. That is, the same mobile phone, each WeChat user can not access the files of other logged in users, the same user is different. appId Files between them cannot be accessed.
File paths to local files are all of the following formats:
{{Protocol name}}://File path
The name of the agreement is in iOS/Android The client is
"wxfile"
, on Developer Tools for"http"
, developers should not be concerned with this difference, nor should they hardcode the full file path in their code.
# Local Temporary Files
Local temporary files can only be generated by calling a specific interface and cannot be written directly to the content. When a local temporary file is generated, it is only guaranteed to be valid for the current lifetime and may not be available after a restart. If you need to ensure that you do not need to download at the next boot, you can do so by FileSystemManager.saveFile() or FileSystemManager.copyFile() Interface to convert local temporary files into local cache files or local user files.
The cleaning strategy of temporary files is: after the Mini Program exits, the system will check the temporary file occupation of the Mini Program, if it does not exceed 2GB, it will not be cleaned, and if it exceeds the upper limit, it will be cleaned up according to the most recent use time from far to near in the dimension of the file. At the same time, it will check the temporary file occupancy of all Mini programs, and if more than 6GB, it will clean up the Mini Program as a dimension.
Therefore, when a developer downloads a temporary file, he or she can download it byFileSystemManager.access()Check if the file exists, reduce duplicate file downloads, and improve the user experience.
# Example
wx.chooseImage({
success: function (res) {
var tempFilePaths = res.tempFilePaths // tempFilePaths Each entry is a local temporary file path
}
})
# Local cache file
Local cache files can only be generated by calling a specific interface and cannot be written directly to the content. After the local cache file is generated, it is still available after reboot. Local cache files can only be accessed by FileSystemManager.saveFile() Interface to save local temporary files obtained.
# Example
fs.saveFile({
tempFilePath: '', // Pass in a local temporary file path
success(res) {
console.log(res.savedFilePath ) // res.savedFilePath For a local cache file path
}
})
Note: The local cache file is the original design,1.7.0
Version starts with a more fully functional local user file that can completely override the local cached file functionality if not required to be compatible with less than 1.7.0
Version, you can do without the use of local cached files.
# Local User Files
The local user file is created from the 1.7.0
Version begins to add concepts. We provide developers with a directory of user files that they can read and write freely. adopt wx.env.USER _DATA_PATH
Can get the path to this directory.
# Example
// Create a file under the local user files directory hello.txt, write content "hello, world"
const fs = wx.getFileSystemManager()
fs.writeFileSync(`${ wx.env.USER _DATA_PATH}/hello.txt`, 'hello, world', 'utf8')
# Read and write permissions
Interfaces, Components | read | write |
---|---|---|
Code package file | Yes | nothing |
Local Temporary Files | Yes | nothing |
Local cache file | Yes | nothing |
Local User Files | Yes | Yes |
# Cleanup strategy
- Local temporary files are only guaranteed during the current life cycle of the Mini Program, and may be cleaned up once the Mini Program is closed, that is, the next cold start is not guaranteed to be available.
- Local cache files and local user files are cleaned up at the same time as code packages, only when the code packages are cleaned up.