# Mini-Program File Management
# 1. Save File to Current Mini-Program Temporary Directory
Supported App Types
Mini-Program✅ Mini-Game✅ H5 Application✅
The SDK supports natively saving files to the Mini-Program's cache directory and returns the local file path for use by the Mini-Program.
/**
Save file to the Mini-Program's cache path
@param fileData The binary data of the file
@param fileName The file name, which must be unique to avoid overwriting
@return The local path of the file, e.g.: finfile://tmp_b118e2e5e2618d4d8bbbb7b0a3ad806a.jpg
*/
- (NSString *)saveFile:(NSData *)fileData fileName:(NSString *)fileName;
# 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 runtime 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 can generate a finfile protocol path by passing in the file name and the desired path type, for use within the Mini-Program. However, the file may not necessarily exist; it should be combined with other APIs to write content to the corresponding file.
Supported App Types
Mini-Program✅ Mini-Game✅ H5 Application✅
/**
Generate finfile protocol path
@param fileName The file name. When pathType is FATFinFileDirUsr, folders are supported, e.g., /aaa/abc.jpg; other types do not support paths
@param pathType The directory type of Finfile, refer to FATFinFilePathType for details
@return The finfile protocol path
*/
- (NSString *)generateFinFilePath:(NSString *)fileName pathType:(FATFinFilePathType)pathType;
Example code:
NSString *filePath = @"abc.txt";
NSString *finfilepath = [[FATClient sharedClient] generateFinFilePath:filePath pathType:FATFinFilePathTmp];
// Return value: finfile://tmp_b118e2e5e2618d4d8bbbb7b0a3ad806a.txt
# 3. Convert Mini-Program File Path to Absolute Path
Supported App Types
Mini-Program✅ Mini-Game✅ H5 Application✅
In some scenarios, the Host-App cannot use the Mini-Program's finfile protocol path directly. This method can be used to convert the finfile protocol path to an absolute path.
For example, when using the Mini-Program's forwarding feature, the returned image path is a Mini-Program file path. This method can convert it to an absolute path, allowing the Host-App to retrieve the image data and then invoke third-party sharing. Another example is in custom APIs, where parameters can pass Mini-Program finfile protocol paths. The Host-App can use this method to convert them to absolute paths and then access the file data.
When using this API with a finfile protocol path, the file must exist; if the file does not exist, nil will be returned. Additionally, the Mini-Program must be running.
/**
Convert file path to absolute path
If it is a network file path, it is returned directly.
If it is a path within the Mini-Program, such as finfile://tmp_b118e2e5e2618d4d8bbbb7b0a3ad806a.jpg, it will be converted to a local absolute path. (If the file does not exist, nil will be returned)
If it is a file path within the Mini-Program package, such as image/abc.jpg, it will also be converted to an absolute path
In other cases, the input path is returned
@param path The file path
@return The absolute path of the file
*/
- (NSString *)fat_absolutePathWithPath:(NSString *)path;
Example code:
NSString *finfilePath = @"finfile://tmp_7C82E6C26B7627334E88355E9D286621.mp4";
NSString *path = [[FATClient sharedClient] fat_absolutePathWithPath:filePath];
# 4. Convert finfile Path to Absolute Path
Supported App Types
Mini-Program✅ Mini-Game✅ H5 Application✅
In some scenarios, when we obtain a Mini-Program's finfile path and need to convert it to a sandbox path, this method can be used to convert it to a sandbox path.
This API is an upgraded version of fat_absolutePathWithPath:, providing more flexibility in converting to absolute paths. Even if the Mini-Program is not running and the finfile does not exist, it can still convert the finfile path to an absolute path. Using this API allows for more flexible file saving into the Mini-Program directory.
/// Convert finfile path to real path. If the input finfilePath is not a finfile protocol, returns nil
/// appletId The Mini-Program id, optional; if not provided, it will attempt to get the current Mini-Program id (may be empty)
/// @param finfilePath The finfile path
/// @param needFileExist Whether the file needs to exist. Pass YES, if the file of the input finfile path does not exist, returns nil; pass NO, returns the corresponding path regardless of file existence
- (NSString *)absolutePathWithAppletId:(NSString *_Nullable)appletId finFilePath:(NSString *)finfilePath needFileExist:(BOOL)needFileExist;
Example code:
NSString *finfilePath = @"finfile://tmp_7C82E6C26B7627334E88355E9D286621.mp4";
NSString *path = [[FATClient sharedClient] absolutePathWithAppletId:@"Mini-Program id" finFilePath:finfilePath needFileExist:YES];
# 5. Get Absolute Path of Files Inside Mini-Program Package
Supported App Types
Mini-Program✅ Mini-Game✅ H5 Application🚫
Pass in the file path inside the Mini-Program package, and return the absolute path of that file.
/**
Get the full path of a file inside the Mini-Program package, or the full path of a temporary file
@param fileName The file name
@return The full path of the file
*/
- (NSString *)getFileAddressWithfileName:(NSString *)fileName;
Example code:
NSString *fileName = @"test.png";
NSString *path = [[FATClient sharedClient] getFileAddressWithfileName:filePath];
# 6. Clear Cache Data for a Specific User
/// Delete cache information for the specified user. If userId is passed as empty, delete cache information for the current user
/// @param userId The user id
/// @param completion Completion callback
- (void)clearUserCache:(NSString *)userId completion:(dispatch_block_t)completion;
Example code:
[[FATClient sharedClient] clearUserCache:@"User id" completion:^{
NSLog(@"Clear user cache successful");
}];