tencent cloud

Feedback

File System

Last updated: 2024-07-12 17:41:32
    The file system consists of a set of storage areas isolated based on Mini Programs and users and a set of appropriate management APIs. A globally unique file system manager is obtained via wx.getFileSystemManager(). The management operations for all file systems are called via FileSystemManager.
    var fs = wx.getFileSystemManager()
    Files are divided into two categories:
    Code package file: A file added in the project directory;
    Local file: A file generated locally by calling an API, or downloaded over the network and stored locally;
    Local files are divided into three types:
    Local temporary file: A file that is temporarily generated and will be recycled at any time. The storage space of local temporary files is unlimited;
    Local cache file: A file generated by a Mini Program after caching a local temporary file via an API. The directory and file name cannot be customized. Normal Mini Programs can store up to 10 MB of local cache and local user files, while game-type Mini Programs can store up to 50 MB;
    Local user file: A file generated by a Mini Program after caching a local temporary file via an API. The directory and file name can be customized. Normal Mini Programs can store up to 10 MB of local user and local cache files, while game-type Mini Programs can store up to 50 MB.

    1. Code Package File

    Due to limited size, code package files are those needed for the first load. Large files and those that require dynamic replacement should not be added to the code package, but should be downloaded to the local device via the download API after the Mini Game is launched.

    1.1 Access code package files

    Code package files are accessed by writing the file path from the project root directory, relative path writing is not supported. For example: /a/b/c, a/b/c are legal, . /a/b/c, . /a/b/c are not legal.

    1.2 Modify code package files

    Files in the code package cannot be dynamically modified or deleted after running. Modifying the code package files requires a version re-release.

    2. Local File

    Local file refers to an individual file storage isolated based on users after the Mini Program is added to the mobile device by the user. A Weixin user cannot access the files of other users who have logged in on the same phone, and a user who have multiple appIds cannot access files under one appId using another appId.
    The file path of a local file follows this format:
    {{Protocol Name}}://File Path
    Note:
    The protocol name is "wxfile" in the Weixin app for iOS/Android and "http" in the Weixin DevTools. Developers can ignore this difference, and should not hard code a complete file path in the code.

    2.1 Local temporary file

    Local temporary file can only be generated by calling a specific API. However, you do not have the permission to write to the file directly. After a local temporary file is generated, it is valid only during the Mini Program's current lifecycle and is not available after the Mini Program restarts. Therefore, you cannot save the local temporary file path for later use. To use a local temporary file later, you can convert it to a local cache file or a local user file via the FileSystemManager.saveFile() or FileSystemManager.copyFile() API.
    Code example:
    wx.chooseImage({
    success: function (res) {
    var tempFilePaths = res.tempFilePaths // Each entry of tempFilePaths is a local temporary file path
    })

    2.2 Local cache file

    Local cache file can only be generated by calling a specific API and cannot be written directly. After a local cache file is generated, it is still available after the Mini Program restarts. Local cache files can only be obtained by saving the local temporary files via the FileSystemManager.saveFile() API.
    Code example:
    fs.saveFile({
    tempFilePath: '', // Pass a local temporary file path
    success(res) {
    console.log(res.savedFilePath) // res.savedFilePath is a local cache file path
    }
    })

    2.3 Local user file

    Local user file is a new concept that has been added since version 1.7.0. We provide a user file directory for developers, who have completely free read/write access to this directory. The path to this directory is available via wx.env.USER_DATA_PATH.
    Code example:
    // 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.4 Read/Write Permission

    API/Component
    Read
    Write
    Code package file
    Yes
    No
    Local temporary file
    Yes
    No
    Local cache file
    Yes
    No
    Local user file
    Yes
    Yes

    2.5 Erasion Rules

    Local temporary files are only guaranteed to persist during the Mini Program's current lifecycle. Once the Mini Program is closed, these files can be erased and may not be available at the next cold start;
    Local cache files and local user files are erased only when the code package is erased.
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support