May 17, 2021 WeChat Mini Program Development Document
A file system is a set of storage isolated by small programs and user dimensions provided by a small program and a set of corresponding management interfaces. Wx.getFileSystemManager() provides access to a globally unique file system manager, and all file system management operations are invoked through FileSystemManager.
var fs = wx.getFileSystemManager()
There are two main categories of documents:
There are three types of local files:
Due to code pack file size limitations, code pack files are suitable for placing files that are required for the first load, and are not recommended for files with large content or that require dynamic replacement, and are recommended for downloading locally with the download interface after the game is started.
Code pack files are accessed by writing file paths from the root of the project and do not support relative paths. S uch as: /a/b/c, a/b/c are legal, . . . / a/b /c . / a/b/c is illegal.
The files within the code pack cannot be dynamically modified or deleted after running, and the modified code pack files need to be re-published.
A local file means that when a small program is added to a phone by the user, it has a separate file storage area that is isolated by the user dimension. T hat is, the same phone, each WeChat user can not access the files of other logged-in users, the same user between different appId files can not access each other.
The file paths for local files are in the following format:
{{协议名}}://文件路径
Where the protocol name is "wxfile" on the iOS/Android client and "http" on the developer tool, the developer does not have to pay attention to the difference and should not hardcode the full file path in the code.
Local temporary files can only be generated by calling a specific interface and cannot be written directly. W hen a local temporary file is generated, it is only valid for the current life cycle and is not available after the restart. T herefore, you cannot store the local temporary file path for your next use. If you need to use it next time, you can use the FileSystemManager.saveFile() or fileSystemManager.copyFile() interface to convert local temporary files to cache files or local user files at cost.
wx.chooseImage({
success: function (res) {
var tempFilePaths = res.tempFilePaths // tempFilePaths 的每一项是一个本地临时文件路径
}
})
Local cache files can only be generated by calling a specific interface and cannot be written directly to the content. A fter the local cache file is generated, it is still available after the restart. Local cache files can only be saved through the FileSystemManager.saveFile() interface.
fs.saveFile({
tempFilePath: '', // 传入一个本地临时文件路径
success(res) {
console.log(res.savedFilePath) // res.savedFilePath 为一个本地缓存文件路径
}
})
Note: Local cache files were originally designed, starting with version 1.7.0, to provide a more complete feature of local user files, to fully overwrite local cache files, and not to use local cache files if you do not need to be compatible with versions below 1.7.0.
Local user files are new concepts that have been added since version 1.7.0. W e provide a user file directory to developers who have complete freedom to read and write to this directory. The wx.env.USER_DATA_PATH to this directory is available through the file.
// 在本地用户文件目录下创建一个文件 hello.txt,写入内容 "hello, world"
const fs = wx.getFileSystemManager()
fs.writeFileSync(`${wx.env.USER_DATA_PATH}/hello.txt`, 'hello, world', 'utf8')
Interfaces, components | Read | Write |
---|---|---|
The code pack file | Yes | No |
Local temporary files | Yes | No |
The file is cached locally | Yes | No |
The local user file | Yes | Yes |