Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat program file system


May 17, 2021 WeChat Mini Program Development Document


Table of contents


File system

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:

  • Code pack file: A code pack file is a file that is added to the project directory.
  • Local files: Files that are generated locally by calling the interface or downloaded over the network and stored locally.

There are three types of local files:

  1. Local temporary files: Files that are generated temporarily and can be recycled at any time. There is no limit to the size of the storage.
  2. Local cache files: Small programs can not customize directories and file names for files produced by caching local temporary files through an interface. In total, ordinary small programs can store up to 10MB, and game-type small programs can store up to 50MB.
  3. Local user files: A small program that caches local temporary files through an interface, allowing custom directories and file names. In total, ordinary small programs can store up to 10MB, and game-type purpose gadgets can store up to 50MB.

The code pack file

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.

Access the code pack file

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. WeChat program file system

Modify the code pack file

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.

The local file

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. WeChat program file system

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

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.

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

The file is cached locally

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.

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

The local user file

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.

Example
// 在本地用户文件目录下创建一个文件 hello.txt,写入内容 "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
The code pack file Yes No
Local temporary files Yes No
The file is cached locally Yes No
The local user file Yes Yes

Clean-up strategy

  • Local temporary files only guarantee that during the current life cycle of the program, once the program is closed it may be cleaned up, i.e. the next cold boot is not guaranteed to be available.
  • Local cache files and local user files are cleaned up at the same time as code packs and are cleaned only when the code package is cleaned.