读 fs:// 路径格式的文件
const fs = ymx.getFileSystemManager();
//读取返回 utf8 字符串
const res = await fs.read({
filePath: 'fs://file/my_dir/my.txt',
encoding: 'utf8'
});
res.data;
//读取返回 Base64 字符串
const res = await fs.read({
filePath: 'fs://file/my_dir/my.txt',
encoding: 'base64'
});
res.data;
//读取返回 Uint8Array 二进制
const res = await fs.read({
filePath: 'fs://file/my_dir/my.txt'
});
res.data;
const fs = ymx.getFileSystemManager();
//本地临时文件
const localZipFile = 'fs://temp/my.zip';
try {
//下载
await ymx.downloadFile({
url: 'https://www.yimenapp.com/doc/www.zip',
filePath: localZipFile
});
alert('已下载 zip 文件,即将解压。');
const targetPath = 'fs://file/my-file/';
//清空目标目录
await fs.remove({ path: targetPath });
//解压到目标目录
await fs.unzip({
zipFilePath: localZipFile,
targetPath: targetPath
});
//递归列举目录文件
const files = await fs.list({
dirPath: targetPath,
recursion: true
});
console.log('解压出的文件', files);
} catch (err) {
alert(`失败 ${err.message}`);
} finally {
//删除临时文件
if (await fs.exist({ path: localZipFile })) {
await fs.remove({ path: localZipFile });
console.log(`已删除 ${localZipFile}`);
}
}
覆盖或追加写文件写 fs:// 路径格式的文件,如果目录不存在会自动创建目录。
const fs = ymx.getFileSystemManager();
//写入 utf8 文本
await fs.write({
filePath: 'fs://file/my_dir/my.txt',
data: 'hello 世界'
});
//写入 Base64
await fs.write({
filePath: 'fs://file/my_dir/my.txt',
data: '5L2g5aW95LiW55WM', //示例为 你好世界 的 Base64 编码
encoding: 'base64'
});
//写入 Uint8Array 二进制
const buffer = new TextEncoder().encode('你好 world');
await fs.write({
filePath: 'fs://file/my_dir/my.txt',
data: buffer
});
await fs.write({
filePath: 'fs://file/my_dir/my.txt',
data: '🐭🐈等你'
});
//追加写入
await fs.write({
filePath: 'fs://file/my_dir/my.txt',
data: '🐕😃来了',
appendMode: true
});
//读取文件内容
await fs.read({
filePath: 'fs://file/my_dir/my.txt',
encoding: 'utf8'
});
文件管理器
请使用 ymx.getFileSystemManager 获取文件管理器
file|cache|temp}/your-sub-path 格式;file保存在应用沙箱文件目录,如fs://file/my-file.txtcache保存在应用沙箱缓存目录,如fs://cache/my-cache.txttemp保存在应用沙箱临时目录,如fs://temp/my-temp.txt