这里先写上tauri官方fs的文档链接fs | Tauri Apps
在使用tauri fs之前,你需要确保在“tauri.conf.json”文件中允许列表中启用了fs:
在tauri.conf.json里面的tauri里面allowlist里添加下面代码,(scope是允许访问的的路径,我这里允许他访问APPDATA里的所有文件)
"fs": {
"scope": ["$APPDATA/*"],
"all": true,
"readFile": true,
"writeFile": true,
"readDir": true,
"copyFile": true,
"createDir": true,
"removeDir": true,
"removeFile": true,
"renameFile": true,
"exists": true
},
import {
appDataDir,
} from "@tauri-apps/api/path";
import {
writeTextFile,
} from "@tauri-apps/api/fs";
一般来说我们可能会这样像下面这样写(下面这个是错误的写法)
writeTextFile("test"+ "\1.txt","这是1.txt的内容",{ dir: BaseDirectory.AppData }
).then((res) => {
//控制台查看打印结果
console.log(res);
});
下面是正确写法,因为我看文档里面的是2个\\来拼接,但是前端是\\才算一个\所以下面代码用到了4个\
writeTextFile("test"+ "\\\\1.txt","这是1.txt的内容",{ dir: BaseDirectory.AppData }
).then((res) => {
//控制台查看打印结果
console.log(res);
});
这里说下BaseDirectory.AppData在本地的路径是C:\Users\admin\AppData\Roaming
然后tauri.conf.json里面的bundle里面的identifier的值是数据目录名,比如我的是com.tauri.build
那么我写入上面的文件之后,那么这个文件目录路径是C:\Users\admin\AppData\Roaming\com.tauri.com\test\1.txt
test是我手动创建的目录,也可以通过代码创建test目录。
下面是官方接口例子
copyFile----复制文件
import { copyFile, BaseDirectory } from '@tauri-apps/api/fs';
// Copy the `$APPCONFIG/app.conf` file to `$APPCONFIG/app.conf.bk`
await copyFile('app.conf', 'app.conf.bk', { dir: BaseDirectory.AppConfig });
createDir----创建目录
import { createDir, BaseDirectory } from '@tauri-apps/api/fs';
// Create the `$APPDATA/users` directory
await createDir('users', { dir: BaseDirectory.AppData, recursive: true });
exists----文件是否已存在
import { exists, BaseDirectory } from '@tauri-apps/api/fs';
// Check if the `$APPDATA/avatar.png` file exists
await exists('avatar.png', { dir: BaseDirectory.AppData });
readBinaryFile----读取二进制文件
import { readBinaryFile, BaseDirectory } from '@tauri-apps/api/fs';
// Read the image file in the `$RESOURCEDIR/avatar.png` path
const contents = await readBinaryFile('avatar.png', { dir: BaseDirectory.Resource });
readDir---查看目录列表
import { readDir, BaseDirectory } from '@tauri-apps/api/fs';
// Reads the `$APPDATA/users` directory recursively
const entries = await readDir('users', { dir: BaseDirectory.AppData, recursive: true });
function processEntries(entries) {
for (const entry of entries) {
console.log(`Entry: ${entry.path}`);
if (entry.children) {
processEntries(entry.children)
}
}
}
readTextFile----读取文件
import { readTextFile, BaseDirectory } from '@tauri-apps/api/fs';
// Read the text file in the `$APPCONFIG/app.conf` path
const contents = await readTextFile('app.conf', { dir: BaseDirectory.AppConfig });
removeDir----删除目录
import { removeDir, BaseDirectory } from '@tauri-apps/api/fs';
// Remove the directory `$APPDATA/users`
await removeDir('users', { dir: BaseDirectory.AppData });
removeFile----删除文件
import { removeFile, BaseDirectory } from '@tauri-apps/api/fs';
// Remove the `$APPConfig/app.conf` file
await removeFile('app.conf', { dir: BaseDirectory.AppConfig });
renameFile----重命名文件
import { renameFile, BaseDirectory } from '@tauri-apps/api/fs';
// Rename the `$APPDATA/avatar.png` file
await renameFile('avatar.png', 'deleted.png', { dir: BaseDirectory.AppData });
writeBinaryFile----写入二进制文件
import { writeBinaryFile, BaseDirectory } from '@tauri-apps/api/fs';
// Write a binary file to the `$APPDATA/avatar.png` path
await writeBinaryFile('avatar.png', new Uint8Array([]), { dir: BaseDirectory.AppData });
writeTextFile----写入文本文件
import { writeTextFile, BaseDirectory } from '@tauri-apps/api/fs';
// Write a text file to the `$APPCONFIG/app.conf` path
await writeTextFile('app.conf', 'file contents', { dir: BaseDirectory.AppConfig });