Skip to content

informat.file 本地文件

概述

使用informat.file对象进行文件操作,所有的文件都会存储在app沙盒环境

术语说明

  • app沙盒环境:app的运行环境。每个app都是在自己的沙盒中运行的,不能直接访问其他app的资源文件和数据。
  • app沙盒中的文件路径:以app目录作为根目录的文件路径,例如,app的沙盒环境的根目录是/

getFile

获取文件信息

javascript
informat.file.getFile(path)
参数类型描述
pathString文件路径

返回值

返回文件信息File

示例

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

获取文件完整路径

javascript
informat.file.getRealPath(path)
参数类型描述
pathString文件路径

返回值

类型String,本地文件完整路径

示例

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

md5

获取文件MD5

javascript
informat.file.md5(path)
参数类型描述
pathString文件路径

返回值

类型String,返回本地文件MD5值

示例

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

create

创建文件,在app的沙盒环境中创建文件

javascript
informat.file.create(path)
参数类型描述
pathString文件路径

返回值

类型String,返回是否创建成功

示例

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

mkdirs

创建文件夹,在app的沙盒环境中创建文件夹

javascript
informat.file.mkdirs(path)
参数类型描述
pathString文件夹路径

返回值

类型Boolean,返回是否创建成功

示例

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

delete

删除文件,在app的沙盒环境中删除文件

如果path对应的文件不存在,系统会报文件不存在错误; 如果path对应的是一个文件夹,不是文件,系统会报文件不存在错误;

javascript
informat.file.delete(path)
参数类型描述
pathString文件路径

返回值

类型Boolean,返回是否删除成功

示例

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

deleteDirectory

删除文件夹,在app的沙盒环境中删除文件文件夹,如果文件夹下面有子文件夹和文件也会一起删除

如果path对应的文件夹不存在,系统会报文件不存在错误; 如果path对应的是一个文件,不是文件夹,系统会报文件不存在错误;

javascript
informat.file.deleteDirectory(path)
参数类型描述
pathString文件夹路径

返回值

类型Boolean,返回是否删除成功

示例

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

exists

判断是否存在文件,在app的沙盒环境中判断是否存在本地文件

javascript
informat.file.exists(path)
参数类型描述
pathString文件路径

返回值

类型Boolean,返回临时文件是否存在

示例

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

isDirectory

判断文件是否是文件夹

javascript
informat.file.isDirectory(path)
参数类型描述
pathString文件路径

返回值

类型Boolean,返回是否是文件夹

示例

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

listFile

返回文件夹下的文件列表

javascript
informat.file.listFile(path)
参数类型描述
pathString文件路径

返回值

类型Array<String>,返回文件夹下的文件列表

示例

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

move

将source文件移动到target

javascript
informat.file.move(source, target)

TIP

如果目标路径已存在,则会移动到目标路径下一级

参数类型描述
sourceString源文件路径
targetString目标路径

返回值

类型Boolean,返回是否成功

示例

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

copy

将source文件复制到target

javascript
informat.file.copy(source, target)
参数类型描述
sourceString源文件路径
targetString目标路径

返回值

类型Boolean,返回是否成功

示例

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

注意

源文件不能是文件夹

zip

压缩文件

javascript
informat.file.zip(sourcePath, targetPath, charsetName, withSrcDir)
参数类型描述
sourcePathString压缩文件的路径
targetPathString需要压缩的文件目录
charsetNameString编码,默认UTF-8,可选
withSrcDirBoolean是否包含被打包目录,可选

示例

javascript
const result1 = informat.file.exists("文件内容/测试内容.jpg");
const result2 = informat.file.exists("文件内容/测试内容1.txt");
informat.file.zip('文件内容', '压缩路径/归档.zip');
informat.file.zip('文件内容', '压缩路径/归档.zip', 'GB2312', true);

unzip

解压缩文件

javascript
informat.file.unzip(sourcePath, targetPath, charsetName)
参数类型描述
sourcePathString需要解压缩的文件路径
targetPathString解压缩的文件夹路径,如果文件夹不存在会自动创建
charsetNameString编码,默认UTF-8 可选

示例

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

readAsBytes

读取文件返回二进制内容

javascript
informat.file.readAsBytes(path)
参数类型描述
pathString文件路径

返回值

类型Array<byte>,文件读取结果,如果文件不存在返回null

示例

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

readAsString

读取文件返回字符串内容

javascript
informat.file.readAsString(path, charset)
参数类型描述
pathString文件路径
charsetString返回内容的字符集

返回值

类型String,文件读取结果,如果文件不存在返回null

示例

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

readAsBase64String

读取文件二进制内容并按照base64编码返回字符串内容

javascript
informat.file.readAsBase64String(path)
参数类型描述
pathString文件路径

返回值

类型String,文件读取结果,如果文件不存在返回null

示例

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

readAsBase64Bytes

读取文件二进制内容并按照base64编码返回二进制内容

javascript
informat.file.readAsBase64Bytes(path)
参数类型描述
pathString文件路径

返回值

类型Array<byte>,文件读取结果,如果文件不存在返回null

示例

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

writeBytes

将二进制内容写入文件

javascript
informat.file.writeBytes(path, content)
参数类型描述
pathString文件路径
contentArray<byte>二进制内容

示例

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

writeString

将字符串写入文件

javascript
informat.file.writeString(path, content, charset)
参数类型描述
pathString文件路径
contentString内容
charsetString字符集

示例

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