Skip to content

File System

The file system is a set of storage isolated by mini-program and user dimensions and a set of corresponding management interfaces provided by the mini-program. Through wx.getFileSystemManager(), you can get the globally unique file system manager. All file system management operations are performed through FileSystemManagerto invoke.

js
var fs = wx.getFileSystemManager();

The 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.

1.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.

1.1Access 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.

1.2Modify 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.

2.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:

js
{{Protocol Name}}://File Path

TIP

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.

2.1Local 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 the FileSystemManager.saveFile() or FileSystemManager.copyFile() interfaces.

The cleaning strategy for temporary files is: after the mini program exits, the system will check the temporary file usage of the mini program. If it does not exceed 2GB, it will not be cleaned up. If it exceeds the upper limit, it will be cleaned up from the most recent use time based on the file dimension. At the same time, the temporary file usage of all mini programs will be checked. If it exceeds 6GB, it will be cleaned up based on the mini program dimension.

Therefore, when downloading temporary files, developers can first check whether the file exists through FileSystemManager.access()to reduce duplicate file downloads and improve user experience

Code example:

js
wx.chooseImage({
  success: function (res) {
    var tempFilePaths = res.tempFilePaths // Each entry of tempFilePaths is a local temporary file path
})

2.2local 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 through FileSystemManager.saveFile()The interface is obtained by saving local temporary files.

Code example:

js
fs.saveFile({
  tempFilePath: "", // Pass a local temporary file path
  success(res) {
    console.log(res.savedFilePath); // res.savedFilePath is a local cache file path
  },
});

2.3local 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:

js
// Create a hello.txt file 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");

2.4Read and write permissions

interface, componentsreadwrite
Code package filehavenone
local temporary filehavenone
local cache filehavenone
local user profilehavehave

2.5Cleanup 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.