# File System

The file system is a set of storage interfaces isolated by Mini Programs and user dimensions and a set of corresponding management interfaces. A globally unique file system manager can be obtained by wx.getFileSystemManager(), and the management operations of all file system are called by FileSystemManager.

var fs = wx.getFileSystemManager()

The files are mainly divided into two categories:

  • Code package files: Code package files refer to files added to a project directory.
  • Local files: Refers to files locally generated by calling the interface or downloaded through the network and stored locally.

There are three types of local files:

  1. Local temporary files: Refer to files that are temporarily generated and will be recycled at any time. There is no limit on storage size.
  2. Local cache files: Refer to files generated by Mini Program after caching local temporary files through interfaces, and the directories and file names can not be customized. The files can not be deleted unless the user actively deletes the Mini Program. A total of up to 50 MB files can be stored with local user files.
  3. Local user files: Refer to files generated by Mini Program after caching local temporary files through interfaces, and the directories and file names can be customized. The files can not be deleted unless the user actively deletes the Mini Program. A total of up to 50 MB files can be stored with local user files.

# Code Package File

Due to the limited size of the code package file, the code package file is suitable for storing files needed for the first time loading. For files with large size of content or requiring dynamic replacement, it is not recommended to add to the code package. It is recommended to download to local storage space using download interface after starting the Mini Game.

# Access the Code Package File

The way to access code package files is to write the file path starting from the root directory of the project. The relative path is not supported. image.png

# Modify the Code Package File

Files in the code package cannot be modified or deleted dynamically after running. Republishing the version is required to modify the code package files.

# Local Files

Local files refer to a separated file storage area after the Mini Program is added to the mobile phone by the user, which is isolated by the user dimension. That means, on the same mobile phone, each Weixin user cannot access to other logged-in user files, and different appIds of the same user cannot access files of each other. Local File Sandbox.png

The file path of the local file is in the following format:

{{protocol name}}://file path

The protocol name is "wxfile" on the iOS/Android client and "http" on the Developer Tools. The developer does not need to pay attention to this difference, and should not hard-code the full file path in the code.

# Local Temporary Files

Local temporary files can only be generated by calling a specific interface, and the content cannot be written directly. After a local temporary file is generated, it is valid only during the current life cycle and becomes unavailable after restart. Therefore, the local temporary file path should not be stored for use the next time. If you need to use it next time, you can use FileSystemManager.saveFile() or FileSystemManager.copyFile() interface to convert local temporary files into local cache files or local user files.

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

# Local Cache File

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

# Example
fs.saveFile({
  tempFilePath: '', // Populate a local temporary file path
  success(res) {
    console.log(res.savedFilePath) // res.savedFilePath is a local cache file path
  }
})

Note: The local cache file is an initial design. Starting from the version 1.7.0, a more complete local user file is provided, which can fully cover the functionality of the local cache file. It is no need to use the local cache file unless there is a need to use compatible versions earlier than 1.7.0.

# Local User File

Local user files are new concepts starting with the version 1.7.0. We provide a directory of user files to developers, and they have full access of read and write to this directory. The path to this directory is available via wx.env.USER_DATA_PATH.

# Example
// Create a file a.txt in the local user file directory and write the content of "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 files Yes No
Local temporary files Yes No
Local cache files Yes No
Local user files Yes Yes