informat.file Local File
Overview
Use the informat.file object for file operations. All files are stored in the app sandbox environment.
Terminology Explanation
- App sandbox environment: The running environment of the app. Each app runs in its own sandbox and cannot directly access resource files and data of other apps.
- File path in app sandbox: A file path with the app directory as the root directory. For example, the root directory of the app's sandbox environment is
/
getFile
Get file information
informat.file.getFile(path);| Parameter | Type | Description |
|---|---|---|
| path | String | File path |
Return Value
Returns file information File
Example
informat.file.getFile("gzb.xlsx");{
"absolute": true,
"absolutePath": "/home/appadmin/informat_home/file_storage/localfiles/g09aj7cus3d8s/ey89pc358ousw/gzb.xlsx",
"canRead": true,
"canWrite": true,
"directory": false,
"file": true,
"isAbsolute": true,
"isDirectory": false,
"isFile": true,
"lastModified": 1683602289249,
"length": 10317,
"name": "gzb.xlsx",
"path": "/home/appadmin/informat_home/file_storage/localfiles/g09aj7cus3d8s/ey89pc358ousw/gzb.xlsx",
"totalSpace": 211243687936,
"usableSpace": 80439672832
}getRealPath
Get the complete file path
informat.file.getRealPath(path);| Parameter | Type | Description |
|---|---|---|
| path | String | File path |
Return Value
Type String, the complete local file path
Example
informat.file.getRealPath("gzb.xlsx");/home/appadmin/informat_home/file_storage/localfiles/g09aj7cus3d8s/ey89pc358ousw/gzb.xlsxmd5
Get file MD5
informat.file.md5(path);| Parameter | Type | Description |
|---|---|---|
| path | String | File path |
Return Value
Type String, returns the MD5 value of the local file
Example
informat.file.md5("gzb.xlsx");eb5958522b1043d715ecf076d5ff3dc9create
Create a file in the app's sandbox environment
informat.file.create(path);| Parameter | Type | Description |
|---|---|---|
| path | String | File path |
Return Value
Type Boolean, returns whether creation was successful
Example
informat.file.create("a.txt");truemkdirs
Create a folder in the app's sandbox environment
informat.file.mkdirs(path);| Parameter | Type | Description |
|---|---|---|
| path | String | Folder path |
Return Value
Type Boolean, returns whether creation was successful
Example
informat.file.mkdirs("a/b/c");truedelete
Delete a file in the app's sandbox environment
If the file corresponding to path does not exist, the system will report a "File not found" error; If path corresponds to a folder instead of a file, the system will report a "File not found" error;
informat.file.delete(path);| Parameter | Type | Description |
|---|---|---|
| path | String | File path |
Return Value
Type Boolean, returns whether deletion was successful
Example
informat.file.delete("a.txt");truedeleteDirectory
Delete a folder in the app's sandbox environment, including all subfolders and files within it
If the folder corresponding to path does not exist, the system will report a "File not found" error; If path corresponds to a file instead of a folder, the system will report a "File not found" error;
informat.file.deleteDirectory(path);| Parameter | Type | Description |
|---|---|---|
| path | String | Folder path |
Return Value
Type Boolean, returns whether deletion was successful
Example
informat.file.deleteDirectory("a");trueexists
Check if a file exists in the app's sandbox environment
informat.file.exists(path);| Parameter | Type | Description |
|---|---|---|
| path | String | File path |
Return Value
Type Boolean, returns whether the temporary file exists
Example
informat.file.exists("a.txt");trueisDirectory
Check if a path is a directory
informat.file.isDirectory(path);| Parameter | Type | Description |
|---|---|---|
| path | String | File path |
Return Value
Type Boolean, returns whether it is a directory
Example
informat.file.isDirectory("a.txt");falselistFile
Return the list of files in a folder
informat.file.listFile(path);| Parameter | Type | Description |
|---|---|---|
| path | String | File path |
Return Value
Type Array<String>, returns the list of files in the folder
Example
informat.file.listFile("");["xxxx123.txt", "1234444", "gzb2.xlsx", "a", "gzb.xlsx"]move
Move source file to target
informat.file.move(source, target);TIP
If the target path already exists, it will be moved to the next level under the target path
| Parameter | Type | Description |
|---|---|---|
| source | String | Source file path |
| target | String | Target path |
Return Value
Type Boolean, returns whether the operation was successful
Example
informat.file.move("a", "b");truecopy
Copy source file to target
informat.file.copy(source, target);| Parameter | Type | Description |
|---|---|---|
| source | String | Source file path |
| target | String | Target path |
Return Value
Type Boolean, returns whether the operation was successful
Example
informat.file.copy("a.txt", "b.txt");trueNote
The source file cannot be a folder
zip
Compress files
informat.file.zip(sourcePath, targetPath, charsetName, withSrcDir);| Parameter | Type | Description |
|---|---|---|
| sourcePath | String | Path of the file to compress |
| targetPath | String | Directory of files to be compressed |
| charsetName | String | Encoding, default UTF-8, optional |
| withSrcDir | Boolean | Whether to include the packaged directory, optional |
Example
const result1 = informat.file.exists("文件内容/测试内容.jpg");
const result2 = informat.file.exists("文件内容/测试内容1.txt");
informat.file.zip("文件内容", "压缩路径/归档.zip");
informat.file.zip("文件内容", "压缩路径/归档.zip", "GB2312", true);Download attachment data from attachment fields and compress folders
/**
* XXX table attachment download and compression script
* Function: Download 2 attachment fields of multiple records and compress them into a zip file
*/
export function downloadAndZip(recordList) {
if (recordList.length == 0) {
return null;
}
const idList = recordList.map((item) => item.id);
const downloadDir = informat.utils.randomUUID();
const zipPath = downloadDir + `.zip`;
try {
console.log("Start executing XXX table attachment download and compression script...");
// 1. Query all records from the empty container/empty vehicle photo table
console.log("Querying table records...");
const records = informat.table.queryList("testTable", {
pageSize: -1,
returnOptionName: true,
filter: {
opt: "and",
conditionList: [{ fieldId: "id", opt: "in", value: idList }],
},
});
console.log(`Query found ${records.length} records`);
if (records.length === 0) {
console.log("No records found, script execution ended");
return;
}
informat.file.mkdirs(downloadDir);
// 2. Process each record
for (let i = 0; i < records.length; i++) {
const record = records[i];
console.log(`\nProcessing record ${i + 1} (ID: ${record.id})`);
// Define 2 attachment fields
const attachmentFields = [
{ field: "floorPhoto", name: "Empty Vehicle Floor Photo" },
{ field: "frontPhoto", name: "Front Photo" },
];
// Create record-specific folder
const recordFolder = downloadDir + `/${record.plateNumber_name}/`;
informat.file.mkdirs(recordFolder);
const downloadedFiles = [];
// 3. Download files for each attachment field
for (const attachmentField of attachmentFields) {
const attachment = record[attachmentField.field];
if (attachment && attachment.id) {
console.log(` Downloading ${attachmentField.name}...`);
try {
let localPath = `${recordFolder}${attachment.name}`;
// Download file from shared storage to local sandbox
informat.storage.download(attachment.path, localPath);
downloadedFiles.push(localPath);
console.log(` ✓ Download successful: ${localPath}`);
} catch (error) {
console.log(` ✗ Download failed: ${attachmentField.name} - ${error.message}`);
}
} else {
console.log(` Skipping ${attachmentField.name} - No attachment`);
}
}
// 4. Compress files
if (downloadedFiles.length > 0) {
console.log(` Starting file compression...${downloadDir}`);
try {
informat.file.zip(downloadDir, zipPath);
console.log(` ✓ Compression successful: ${zipPath}`);
console.log(` ✓ Compressed ${downloadedFiles.length} files in total`);
} catch (error) {
console.log(` ✗ Compression failed: ${error.message}`);
}
} else {
console.log(`No attachments to compress, skipping this record`);
}
}
informat.file.deleteDirectory(downloadDir);
console.log("\nScript execution completed!");
} catch (error) {
informat.file.deleteDirectory(downloadDir);
console.error("Script execution error:", error);
throw error;
}
return zipPath;
}unzip
Decompress files
informat.file.unzip(sourcePath, targetPath, charsetName);| Parameter | Type | Description |
|---|---|---|
| sourcePath | String | Path of the file to decompress |
| targetPath | String | Directory path to decompress to. If the directory doesn't exist, it will be automatically created |
| charsetName | String | Encoding, default UTF-8, optional |
Example
informat.file.unzip("压缩路径/归档.zip", "解压路径/所有文件");
informat.file.unzip("压缩路径/归档.zip", "解压路径/所有文件", "GB2312");readAsBytes
Read a file and return binary content
informat.file.readAsBytes(path);| Parameter | Type | Description |
|---|---|---|
| path | String | File path |
Return Value
Type Array<byte>, file reading result. Returns null if the file does not exist
Example
informat.file.readAsBytes("a.txt");readAsString
Read a file and return string content
informat.file.readAsString(path, charset);| Parameter | Type | Description |
|---|---|---|
| path | String | File path |
| charset | String | Character set of the returned content |
Return Value
Type String, file reading result. Returns null if the file does not exist
Example
informat.file.readAsString("a.txt", "utf-8");hello informatreadAsBase64String
Read file binary content and return string content encoded in base64
informat.file.readAsBase64String(path);| Parameter | Type | Description |
|---|---|---|
| path | String | File path |
Return Value
Type String, file reading result. Returns null if the file does not exist
Example
const file = "a.txt";
informat.file.writeString(file, "hello informat", "utf-8");
informat.file.readAsBase64String(file);aGVsbG8gaW5mb3JtYXQ=readAsBase64Bytes
Read file binary content and return binary content encoded in base64
informat.file.readAsBase64Bytes(path);| Parameter | Type | Description |
|---|---|---|
| path | String | File path |
Return Value
Type Array<byte>, file reading result. Returns null if the file does not exist
Example
const file = "a.txt";
informat.file.writeString(file, "hello informat", "utf-8");
informat.file.readAsBase64Bytes(file);writeBytes
Write binary content to a file
informat.file.writeBytes(path, content);| Parameter | Type | Description |
|---|---|---|
| path | String | File path |
| content | Array<byte> | Binary content |
Example
const readResult = informat.file.readAsBytes("x90.txt");
informat.file.writeBytes("b.txt", readResult);writeString
Write string content to a file
informat.file.writeString(path, content, charset);| Parameter | Type | Description |
|---|---|---|
| path | String | File path |
| content | String | Content |
| charset | String | Character set |
Example
informat.file.writeString("c.txt", "hello informat", "utf-8");
