# Mini-Program File Management

# 1. Save File to Current Mini-Program Temporary Directory

The SDK supports natively saving files to the Mini-Program's cache directory, then returns the local path of the file for use by the Mini-Program.

/**
 * Save file to the Mini-Program's cache path
 * @param appId Mini-Program ID
 * @param apiServer API server address
 * @param fileData Binary data of the file
 * @param fileName File name, must ensure uniqueness to avoid overwriting
 * @param appRunId Mini-Program run ID, defaults to empty string
 * @return Local path of the file, e.g.: finfile://tmp/b118e2e5e2618d4d8bbbb7b0a3ad806a.jpg
 */
 async saveFile(appId: string, apiServer: string, fileData: ArrayBuffer, fileName: string, appRunId: string = ''): Promise<IFinApplet.IFinAppResult<string>>;

# 2. Generate finfile Protocol Path

There are three types of finfile protocol paths in Mini-Programs: finfile://tmp/, finfile://store/, and finfile://usr/. The tmp type path is saved under the Mini-Program's run directory and will be cleared upon the next cold start of the Mini-Program; the store path is persisted and will only be deleted when the Mini-Program is removed; the usr type path is also persisted, but allows custom file names and save directories.

This function accepts a file name and the path type to be saved, generating a finfile protocol path for use in the Mini-Program. However, the file may not necessarily exist; it should be used in conjunction with other APIs to write content to the corresponding file.

/**
 * Generate a finfile protocol path
 * @param filePath File path
 * @param pathType Finfile directory type, refer to FATFinFilePathType for details
 * @return Path in finfile protocol, e.g.: finfile://tmp/b118e2e5e2618d4d8bbbb7b0a3ad806a.jpg
 */
 generateFinFilePath(filePath: string, pathType: EFinFilePathType): string;

Example code:

const finfilepath = client.generateFinFilePath('abc.txt',EFinFilePathType.tmp)
// Return value: finfile://tmp/b118e2e5e2618d4d8bbbb7b0a3ad806a.txt

# 3. Convert finfile Path to Absolute Path

In some scenarios, when you obtain a Mini-Program's finfile path and need to convert it to a sandbox path, you can use this method to perform the conversion.

/**
 * Get the sandbox path based on the given path
 * @param appId Mini-Program ID
 * @param apiServer API server address
 * @param schemaFilePath File path
 */
async absolutePathWithPath(appId: string, apiServer: string, schemaFilePath: string): Promise<IFinApplet.IFinAppResult<string>>;

Example code:

const appId = 'appId'
const apiServer = 'apiServer'
const schemaFilePath = 'finfile://tmp/b118e2e5e2618d4d8bbbb7b0a3ad806a.txt'
const finfilepath = await client.absolutePathWithPath(appId,apiServer,schemaFilePath)

# 4. Get the Sandbox Path of the Mini-Program Temporary Directory

In some scenarios where you need to access or manipulate files in the Mini-Program's temporary directory, you can use this method to convert it to a sandbox path.

/**
 * Get the Mini-Program temporary path
 * @param appId Mini-Program ID
 * @param apiServer API server address
 */
async getAppletTempPath(appId: string, apiServer: string): Promise<IFinApplet.IFinAppResult<string>>;
© FinClip with ❤ , Since 2017