Skip to content

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

javascript
informat.file.getFile(path);
ParameterTypeDescription
pathStringFile path

Return Value

Returns file information File

Example

javascript
informat.file.getFile("gzb.xlsx");
json
{
  "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

javascript
informat.file.getRealPath(path);
ParameterTypeDescription
pathStringFile path

Return Value

Type String, the complete local file path

Example

javascript
informat.file.getRealPath("gzb.xlsx");
text
/home/appadmin/informat_home/file_storage/localfiles/g09aj7cus3d8s/ey89pc358ousw/gzb.xlsx

md5

Get file MD5

javascript
informat.file.md5(path);
ParameterTypeDescription
pathStringFile path

Return Value

Type String, returns the MD5 value of the local file

Example

javascript
informat.file.md5("gzb.xlsx");
text
eb5958522b1043d715ecf076d5ff3dc9

create

Create a file in the app's sandbox environment

javascript
informat.file.create(path);
ParameterTypeDescription
pathStringFile path

Return Value

Type Boolean, returns whether creation was successful

Example

javascript
informat.file.create("a.txt");
text
true

mkdirs

Create a folder in the app's sandbox environment

javascript
informat.file.mkdirs(path);
ParameterTypeDescription
pathStringFolder path

Return Value

Type Boolean, returns whether creation was successful

Example

javascript
informat.file.mkdirs("a/b/c");
text
true

delete

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;

javascript
informat.file.delete(path);
ParameterTypeDescription
pathStringFile path

Return Value

Type Boolean, returns whether deletion was successful

Example

javascript
informat.file.delete("a.txt");
text
true

deleteDirectory

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;

javascript
informat.file.deleteDirectory(path);
ParameterTypeDescription
pathStringFolder path

Return Value

Type Boolean, returns whether deletion was successful

Example

javascript
informat.file.deleteDirectory("a");
text
true

exists

Check if a file exists in the app's sandbox environment

javascript
informat.file.exists(path);
ParameterTypeDescription
pathStringFile path

Return Value

Type Boolean, returns whether the temporary file exists

Example

javascript
informat.file.exists("a.txt");
text
true

isDirectory

Check if a path is a directory

javascript
informat.file.isDirectory(path);
ParameterTypeDescription
pathStringFile path

Return Value

Type Boolean, returns whether it is a directory

Example

javascript
informat.file.isDirectory("a.txt");
text
false

listFile

Return the list of files in a folder

javascript
informat.file.listFile(path);
ParameterTypeDescription
pathStringFile path

Return Value

Type Array<String>, returns the list of files in the folder

Example

javascript
informat.file.listFile("");
json
["xxxx123.txt", "1234444", "gzb2.xlsx", "a", "gzb.xlsx"]

move

Move source file to target

javascript
informat.file.move(source, target);

TIP

If the target path already exists, it will be moved to the next level under the target path

ParameterTypeDescription
sourceStringSource file path
targetStringTarget path

Return Value

Type Boolean, returns whether the operation was successful

Example

javascript
informat.file.move("a", "b");
text
true

copy

Copy source file to target

javascript
informat.file.copy(source, target);
ParameterTypeDescription
sourceStringSource file path
targetStringTarget path

Return Value

Type Boolean, returns whether the operation was successful

Example

javascript
informat.file.copy("a.txt", "b.txt");
text
true

Note

The source file cannot be a folder

zip

Compress files

javascript
informat.file.zip(sourcePath, targetPath, charsetName, withSrcDir);
ParameterTypeDescription
sourcePathStringPath of the file to compress
targetPathStringDirectory of files to be compressed
charsetNameStringEncoding, default UTF-8, optional
withSrcDirBooleanWhether to include the packaged directory, optional

Example

javascript
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

javascript
/**
 * 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

javascript
informat.file.unzip(sourcePath, targetPath, charsetName);
ParameterTypeDescription
sourcePathStringPath of the file to decompress
targetPathStringDirectory path to decompress to. If the directory doesn't exist, it will be automatically created
charsetNameStringEncoding, default UTF-8, optional

Example

javascript
informat.file.unzip("压缩路径/归档.zip", "解压路径/所有文件");
informat.file.unzip("压缩路径/归档.zip", "解压路径/所有文件", "GB2312");

readAsBytes

Read a file and return binary content

javascript
informat.file.readAsBytes(path);
ParameterTypeDescription
pathStringFile path

Return Value

Type Array<byte>, file reading result. Returns null if the file does not exist

Example

javascript
informat.file.readAsBytes("a.txt");

readAsString

Read a file and return string content

javascript
informat.file.readAsString(path, charset);
ParameterTypeDescription
pathStringFile path
charsetStringCharacter set of the returned content

Return Value

Type String, file reading result. Returns null if the file does not exist

Example

javascript
informat.file.readAsString("a.txt", "utf-8");
text
hello informat

readAsBase64String

Read file binary content and return string content encoded in base64

javascript
informat.file.readAsBase64String(path);
ParameterTypeDescription
pathStringFile path

Return Value

Type String, file reading result. Returns null if the file does not exist

Example

javascript
const file = "a.txt";
informat.file.writeString(file, "hello informat", "utf-8");
informat.file.readAsBase64String(file);
text
aGVsbG8gaW5mb3JtYXQ=

readAsBase64Bytes

Read file binary content and return binary content encoded in base64

javascript
informat.file.readAsBase64Bytes(path);
ParameterTypeDescription
pathStringFile path

Return Value

Type Array<byte>, file reading result. Returns null if the file does not exist

Example

javascript
const file = "a.txt";
informat.file.writeString(file, "hello informat", "utf-8");
informat.file.readAsBase64Bytes(file);

writeBytes

Write binary content to a file

javascript
informat.file.writeBytes(path, content);
ParameterTypeDescription
pathStringFile path
contentArray<byte>Binary content

Example

javascript
const readResult = informat.file.readAsBytes("x90.txt");
informat.file.writeBytes("b.txt", readResult);

writeString

Write string content to a file

javascript
informat.file.writeString(path, content, charset);
ParameterTypeDescription
pathStringFile path
contentStringContent
charsetStringCharacter set

Example

javascript
informat.file.writeString("c.txt", "hello informat", "utf-8");