# File System

The document system is a set of storage and a corresponding management interface provided by Weixin Mini Program.Through wx.getFileSystemManager () The globally unique file system manager is available, and all document system management operations are called by FileSystemManager .

var fs = wx.getFileSystemManager()

The documents are divided into two main categories:

  • Code pack files: Code pack files are files that are added to the project directory.
  • Local files: files that are generated locally by calling the interface or downloaded over the network to be stored locally.

Local files are divided into three types:

  1. Local temporary files: files that are created temporarily and will be recovered at any time. A maximum of 4 GB is stored at runtime. After the end of the run, if more than 2 GB has been used, at least 2 GB will be cleaned from far to near in the file dimension according to the most recent use time.
  2. Local cache file: Weixin Mini Program A file generated by caching a local temporary file through an interface. Cannot define a directory and file name. With a total of local user files, Mini Programs (including MiniGame) can store up to 200MB.
  3. Local user file: Weixin Mini Program A file created by caching a local temporary file through an interface, allowing custom directories and file names. Together with local cache files, Mini Programs (including MiniGame) can store up to 200 MB.

# Code package file

Because of the code pack file size limitation, the code pack files are suitable for files that are needed for the first time loading. It is not recommended to add to the code pack for files that have larger content or need to be dynamically replaced. It is recommended to download locally using the download interface after MiniGame starts.

# Access the 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.For example,/ a / b / c,a / b / c are all legal,. / a / b / c``.. / a / b / c [[T is not legal. image.png

# Modify the code package file

Files within code packages cannot be dynamically modified or deleted after running. Modifying code packages files requires a redistribution of the version.

# Local files

A local file refers to a separate file storage area that has been added to the phone by the user, separated by the user dimension.That is, the same phone, each WeChat user cannot access the files of other users, and the files between different appId of the same user cannot access each other. Local file sandbox.png

File paths for local files are in the following formats:

{{协议名}}://文件路径

Among them, the protocol name is"wxfile"on iOS / Android and on developer tools. "http"`, developers don't need to focus on this difference and shouldn't hardcode full file paths in their code.

# Local temporary files

Local temporary files can only be created by calling a specific interface and cannot be directly written to the content. Once a local temporary file is created, it is only guaranteed to be valid for the current life cycle and is not necessarily available after a reboot.If you need to ensure that no download is required at the next boot, you can use the FileSystemManager.saveFile () or FileSystemManager.copyFile () Interface converts local temporary files into local cache files or local user files.

The temporary file cleanup policy is: Weixin Mini Program After exit, the system checks the temporary file usage of the Mini Program, does not clean up if it does not exceed 2GB, and over the upper limit, cleans up from far to near in the file dimension by the most recent usage time. 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, developers can download temporary files through FileSystemManager.access () Check the existence of the file to reduce duplicate file downloads and improve the user experience.

# Example

wx.chooseImage({
  success: function (res) {
    var tempFilePaths = res.tempFilePaths // tempFilePaths 的每一项是一个本地临时文件路径
  }
})

# Local cache files

Local cache files can only be generated by calling a specific interface and cannot be directly written to the content. After the local cache file is created, it is still available after a reboot.Local cache files can only be obtained by saving local temporary files through the FileSystemManager.saveFile () interface.

# Example

fs.saveFile({
  tempFilePath: '', // 传入一个本地临时文件路径
  success(res) {
    console.log(res.savedFilePath) // res.savedFilePath 为一个本地缓存文件路径
  }
})

Note: Local cache files are originally designed,1.7.0Version starts with a more complete local user file that can fully cover the functionality of local cache files and can be used without local cache files if compatibility is not required below1.7.0versions.

# Local user files

Local user files are a new concept from1.7.0release.We have provided a user file directory to the developers, who have completely free access to this directory.The path to this directory can be obtained bywx.env.USER_DATA_PATH.

# Example

// Create a file hello.txt in the local user files directory, writing the 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 files Yes nothing
Local user files Yes Yes

# Cleanup Strategy

  • Local temporary files are only guaranteed in the current life cycle of Weixin Mini Program, once the Mini Program is closed, it may be cleaned up, that is, the next cold start is not guaranteed to be available.
  • Local cache files and local user files are cleaned at the same time as code packages, and only when the code packages are cleaned.