跳到内容
Tauri

@tauri-apps/plugin-fs

访问文件系统。

此模块可防止路径遍历,不允许使用父目录访问器(即不允许使用“/usr/path/to/../file”或“../path/to/file”路径)。使用此 API 访问的路径必须是相对于其中一个基本目录的路径,或者使用路径 API 创建的路径。

该 API 具有作用域配置,强制您使用 glob 模式限制可以访问的路径。

作用域配置是一个 glob 模式数组,描述允许的文件/目录路径。例如,此作用域配置允许所有启用的 fs API(仅)访问$APPDATA 目录数据库目录中的文件。

{
"permissions": [
{
"identifier": "fs:scope",
"allow": [{ "path": "$APPDATA/databases/*" }]
}
]
}

作用域也可以通过使用 API 的标识符而不是 fs:scope 应用于特定的 fs API。

{
"permissions": [
{
"identifier": "fs:allow-exists",
"allow": [{ "path": "$APPDATA/databases/*" }]
}
]
}

请注意 $APPDATA 变量的使用。该值在运行时注入,解析为应用程序数据目录

可用变量包括:$APPCONFIG, $APPDATA, $APPLOCALDATA, $APPCACHE, $APPLOG, $AUDIO, $CACHE, $CONFIG, $DATA, $LOCALDATA, $DESKTOP, $DOCUMENT, $DOWNLOAD, $EXE, $FONT, $HOME, $PICTURE, $PUBLIC, $RUNTIME, $TEMPLATE, $VIDEO, $RESOURCE, $TEMP

尝试执行任何未在作用域中配置 URL 的 API 将导致因拒绝访问而导致 Promise 拒绝。

2.0.0

AppCache: 16;

有关更多信息,请参阅 appCacheDir。

来源: 未定义

AppConfig: 13;

有关更多信息,请参阅 appConfigDir。

来源: 未定义

AppData: 14;

有关更多信息,请参阅 appDataDir。

来源: 未定义

AppLocalData: 15;

有关更多信息,请参阅 appLocalDataDir。

来源: 未定义

AppLog: 17;

有关更多信息,请参阅 appLogDir。

来源: 未定义

Audio: 1;

有关更多信息,请参阅 audioDir。

来源: 未定义

Cache: 2;

有关更多信息,请参阅 cacheDir。

来源: 未定义

Config: 3;

有关更多信息,请参阅 configDir。

来源: 未定义

Data: 4;

有关更多信息,请参阅 dataDir。

来源: 未定义

Desktop: 18;

有关更多信息,请参阅 desktopDir。

来源: 未定义

Document: 6;

有关更多信息,请参阅 documentDir。

来源: 未定义

Download: 7;

有关更多信息,请参阅 downloadDir。

来源: 未定义

Executable: 19;

有关更多信息,请参阅 executableDir。

来源: 未定义

Font: 20;

有关更多信息,请参阅 fontDir。

来源: 未定义

Home: 21;

有关更多信息,请参阅 homeDir。

来源: 未定义

LocalData: 5;

有关更多信息,请参阅 localDataDir。

来源: 未定义

Picture: 8;

有关更多信息,请参阅 pictureDir。

来源: 未定义

Public: 9;

有关更多信息,请参阅 publicDir。

来源: 未定义

Resource: 11;

有关更多信息,请参阅 resourceDir。

来源: 未定义

Runtime: 22;

有关更多信息,请参阅 runtimeDir。

来源: 未定义

Temp: 12;

有关更多信息,请参阅 tempDir。

来源: 未定义

Template: 23;

有关更多信息,请参阅 templateDir。

来源: 未定义

Video: 10;

有关更多信息,请参阅 videoDir。

来源: 未定义


Current: 1;

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L80

End: 2;

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L81

Start: 0;

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L79

Tauri 用于读写文件的抽象。

2.0.0

  • Resource

new FileHandle(rid): FileHandle
参数类型
rid数字

FileHandle

Resource.constructor

来源: 未定义

get rid(): number

数字

Resource.rid

来源: 未定义

close(): Promise<void>

销毁并从内存中清理此资源。您不应再对此对象调用任何方法,并应放弃对它的任何引用。

Promise<void>

Resource.close

来源: 未定义

read(buffer): Promise<null | number>

读取最多 p.byteLength 字节到 p 中。如果遇到任何错误,它将解析为读取的字节数(0 < n <= p.byteLength),否则将被拒绝。即使 read() 解析为 n < p.byteLength,它在调用期间也可能将 p 的所有内容用作暂存空间。如果某些数据可用但不足 p.byteLength 字节,则 read() 通常会解析为可用数据而不是等待更多数据。

read() 遇到文件结束条件时,它将解析为 EOF (null)。

read() 遇到错误时,它将拒绝并返回错误。

调用者在考虑 EOF (null) 之前,应始终处理返回的 n > 0 字节。这样做可以正确处理在读取一些字节后发生的 I/O 错误以及两种允许的 EOF 行为。

参数类型
缓冲区Uint8Array

Promise<null | number>

import { open, BaseDirectory } from "@tauri-apps/plugin-fs"
// if "$APPCONFIG/foo/bar.txt" contains the text "hello world":
const file = await open("foo/bar.txt", { baseDir: BaseDirectory.AppConfig });
const buf = new Uint8Array(100);
const numberOfBytesRead = await file.read(buf); // 11 bytes
const text = new TextDecoder().decode(buf); // "hello world"
await file.close();

2.0.0

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L314

seek(offset, whence): Promise<number>

Seek 根据 whence 设置下一个 read()write() 的偏移量:Start 表示相对于文件开头,Current 表示相对于当前偏移量,End 表示相对于文件末尾。Seek 解析为相对于文件开头的新偏移量。

定位到文件开头之前的偏移量是错误的。定位到任何正偏移量都是合法的,但后续 I/O 操作对底层对象的行为取决于实现。它返回光标位置的数量。

参数类型
偏移量数字
原因SeekMode

Promise<number>

import { open, SeekMode, BaseDirectory } from '@tauri-apps/plugin-fs';
// Given hello.txt pointing to file with "Hello world", which is 11 bytes long:
const file = await open('hello.txt', { read: true, write: true, truncate: true, create: true, baseDir: BaseDirectory.AppLocalData });
await file.write(new TextEncoder().encode("Hello world"));
// Seek 6 bytes from the start of the file
console.log(await file.seek(6, SeekMode.Start)); // "6"
// Seek 2 more bytes from the current position
console.log(await file.seek(2, SeekMode.Current)); // "8"
// Seek backwards 2 bytes from the end of the file
console.log(await file.seek(-2, SeekMode.End)); // "9" (e.g. 11-2)
await file.close();

2.0.0

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L369

stat(): Promise<FileInfo>

返回此文件的 FileInfo

Promise<FileInfo>

import { open, BaseDirectory } from '@tauri-apps/plugin-fs';
const file = await open("file.txt", { read: true, baseDir: BaseDirectory.AppLocalData });
const fileInfo = await file.stat();
console.log(fileInfo.isFile); // true
await file.close();

2.0.0

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L391

truncate(len?): Promise<void>

截断或扩展此文件,以达到指定的 len。如果未指定 len,则截断整个文件内容。

参数类型
长度?数字

Promise<void>

import { open, BaseDirectory } from '@tauri-apps/plugin-fs';
// truncate the entire file
const file = await open("my_file.txt", { read: true, write: true, create: true, baseDir: BaseDirectory.AppLocalData });
await file.truncate();
// truncate part of the file
const file = await open("my_file.txt", { read: true, write: true, create: true, baseDir: BaseDirectory.AppLocalData });
await file.write(new TextEncoder().encode("Hello World"));
await file.truncate(7);
const data = new Uint8Array(32);
await file.read(data);
console.log(new TextDecoder().decode(data)); // Hello W
await file.close();

2.0.0

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L423

write(data): Promise<number>

data.byteLength 字节从 data 写入底层数据流。它解析为从 data 写入的字节数(0 <= n <= data.byteLength),或者在写入过早停止时以遇到的错误拒绝。write() 如果解析为 n < data.byteLength,则必须以非 null 错误拒绝。write() 决不能修改切片数据,即使是临时修改也不可以。

参数类型
数据Uint8Array

Promise<number>

import { open, write, BaseDirectory } from '@tauri-apps/plugin-fs';
const encoder = new TextEncoder();
const data = encoder.encode("Hello world");
const file = await open("bar.txt", { write: true, baseDir: BaseDirectory.AppLocalData });
const bytesWritten = await file.write(data); // 11
await file.close();

2.0.0

来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L450

2.0.0

属性类型描述定义于
fromPathBaseDir?BaseDirectoryfromPath 的基本目录。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L586
toPathBaseDir?BaseDirectorytoPath 的基本目录。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L588

2.0.0

属性类型描述定义于
baseDir?BaseDirectorypath 的基本目录来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L463

2.0.0

属性类型描述继承自 (Inherited from)定义于
baseDir?BaseDirectorypath 的基本目录WatchOptions.baseDir来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1170
delayMs?数字去抖延迟-来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1178
recursive?布尔值 (boolean)递归监视目录WatchOptions.recursive来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1168

一个磁盘条目,可以是文件、目录或符号链接。

这是 readDir 的结果。

2.0.0

属性类型描述定义于
isDirectory布尔值 (boolean)指定此条目是否为目录。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L677
isFile布尔值 (boolean)指定此条目是否为文件。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L679
isSymlink布尔值 (boolean)指定此条目是否为符号链接。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L681
name字符串条目的名称(带扩展名的文件名或目录名)。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L675

2.0.0

属性类型描述定义于
baseDir?BaseDirectorypath 的基本目录。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1135

FileInfo 描述一个文件,由 statlstatfstat 返回。

2.0.0

属性类型描述定义于
atimenull | Date文件的上次访问时间。这对应于 Unix 上 stat 中的 atime 字段和 Windows 上 ftLastAccessTime。并非所有平台上都可用。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L120
birthtimenull | Date文件的创建时间。这对应于 Mac/BSD 上 stat 中的 birthtime 字段和 Windows 上 ftCreationTime。并非所有平台上都可用。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L126
blksizenull | number文件系统 I/O 的块大小。平台特定 - Windows: 不支持。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L203
blocksnull | number分配给文件的块数,以 512 字节为单位。平台特定 - Windows:不支持。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L211
devnull | number包含文件的设备的 ID。平台特定 - Windows:不支持。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L146
fileAttributesnull | number此字段包含文件或目录的文件系统属性信息。有关可能的值及其描述,请参阅 Windows 开发中心中的文件属性常量。平台特定 - macOS / Linux / Android / iOS:不支持。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L138
gidnull | number此文件所有者的组 ID。平台特定 - Windows:不支持。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L187
inonull | number索引节点号。平台特定 - Windows:不支持。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L154
isDirectory布尔值 (boolean)如果这是常规目录的信息,则为 True。与 FileInfo.isFileFileInfo.isSymlink 互斥。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L99
isFile布尔值 (boolean)如果这是常规文件的信息,则为 True。与 FileInfo.isDirectoryFileInfo.isSymlink 互斥。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L94
isSymlink布尔值 (boolean)如果这是符号链接的信息,则为 True。与 FileInfo.isFileFileInfo.isDirectory 互斥。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L104
modenull | number包含此文件/目录标准 Unix 权限的底层原始 st_mode 位。平台特定 - Windows:不支持。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L163
mtimenull | Date文件的最后修改时间。这对应于 Linux/Mac OS 上 statmtime 字段和 Windows 上的 ftLastWriteTime。这可能并非在所有平台上都可用。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L114
nlinknull | number指向此文件的硬链接数。平台特定 - Windows:不支持。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L171
rdevnull | number此文件的设备 ID。平台特定 - Windows:不支持。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L195
readonly布尔值 (boolean)这是否是只读(不可写入)文件。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L128
size数字文件大小,以字节为单位。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L108
uidnull | number此文件所有者的用户 ID。平台特定 - Windows:不支持。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L179

2.0.0

属性类型描述定义于
baseDir?BaseDirectorypath 的基本目录来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L631
mode?数字创建目录时使用的权限(默认为 0o777,在进程的 umask 之前)。在 Windows 上被忽略。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L625
recursive?布尔值 (boolean)默认为 false。如果设置为 true,则表示任何中间目录也将被创建(类似于 shell 命令 mkdir -p)。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L629

2.0.0

属性类型描述定义于
append?布尔值 (boolean)设置追加模式的选项。此选项设置为 true 时,表示写入将追加到文件而不是覆盖以前的内容。请注意,设置 { write: true, append: true } 与仅设置 { append: true } 具有相同的效果。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L518
baseDir?BaseDirectorypath 的基本目录来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L546
create?布尔值 (boolean)设置允许创建新文件的选项,如果指定路径不存在。需要写入或追加访问才能使用。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L531
createNew?布尔值 (boolean)默认为 false。如果设置为 true,则目标位置不允许存在文件、目录或符号链接。需要写入或追加访问才能使用。当 createNew 设置为 true 时,create 和 truncate 将被忽略。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L538
mode?数字如果创建文件时使用的权限(默认为 0o666,在进程的 umask 之前)。在 Windows 上被忽略。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L544
read?布尔值 (boolean)设置读取访问权限的选项。此选项设置为 true 时,表示如果打开文件,则文件应可读。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L504
truncate?布尔值 (boolean)设置截断以前文件的选项。如果成功打开文件并设置此选项,则如果文件已存在,它将把文件截断为 0 大小。文件必须以写入访问权限打开才能使截断生效。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L525
write?布尔值 (boolean)设置写入访问权限的选项。此选项设置为 true 时,表示如果打开文件,则文件应可写入。如果文件已存在,则对其进行的任何写入调用将覆盖其内容,默认情况下不会截断它。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L511

2.0.0

属性类型描述定义于
baseDir?BaseDirectorypath 的基本目录来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L663

2.0.0

属性类型描述定义于
baseDir?BaseDirectorypath 的基本目录来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L725

2.0.0

属性类型描述定义于
baseDir?BaseDirectorypath 的基本目录来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L862
recursive?布尔值 (boolean)默认为 false。如果设置为 true,即使是非空目录,路径也将被删除。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L860

2.0.0

属性类型描述定义于
newPathBaseDir?BaseDirectorynewPath 的基本目录。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L898
oldPathBaseDir?BaseDirectoryoldPath 的基本目录。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L896

2.0.0

属性类型描述定义于
baseDir?BaseDirectorypath 的基本目录。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L940

2.0.0

属性类型描述定义于
baseDir?BaseDirectorypath 的基本目录。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L999

2.0.0

属性类型定义于
attrs未知来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1187
paths字符串[]来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1186
typeWatchEventKind来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1185

2.0.0

属性类型描述定义于
baseDir?BaseDirectorypath 的基本目录来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1170
recursive?布尔值 (boolean)递归监视目录来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1168

2.0.0

属性类型描述定义于
append?布尔值 (boolean)默认为 false。如果设置为 true,则将追加到文件而不是覆盖以前的内容。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1043
baseDir?BaseDirectorypath 的基本目录来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1051
create?布尔值 (boolean)设置允许创建新文件的选项,如果指定路径不存在(默认为 true)。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1045
createNew?布尔值 (boolean)设置创建新文件的选项,如果文件已存在则失败。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1047
mode?数字文件权限。在 Windows 上被忽略。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1049

type UnwatchFn: () => void;

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1252


type WatchEventKind:
| "any"
| object
| object
| object
| object
| "other";

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1193


type WatchEventKindAccess: object | object | object | object;

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1204


type WatchEventKindCreate: object | object | object | object;

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1213


type WatchEventKindModify:
| object
| object
| object
| object
| object;

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1222


type WatchEventKindRemove: object | object | object | object;

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1242

function copyFile(
fromPath,
toPath,
options?): Promise<void>

将一个文件的内容和权限复制到另一个指定路径,默认情况下如果需要则创建新文件,否则覆盖。

参数类型
fromPathstring | URL
toPathstring | URL
选项?CopyFileOptions

Promise<void>

import { copyFile, BaseDirectory } from '@tauri-apps/plugin-fs';
await copyFile('app.conf', 'app.conf.bk', { fromPathBaseDir: BaseDirectory.AppConfig, toPathBaseDir: BaseDirectory.AppConfig });

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L601


function create(path, options?): Promise<FileHandle>

如果文件不存在,则创建文件;如果文件已存在,则截断文件,并解析为 FileHandle 的实例。

参数类型
pathstring | URL
选项?CreateOptions

Promise<FileHandle>

import { create, BaseDirectory } from "@tauri-apps/plugin-fs"
const file = await create("foo/bar.txt", { baseDir: BaseDirectory.AppConfig });
await file.write(new TextEncoder().encode("Hello world"));
await file.close();

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L480


function exists(path, options?): Promise<boolean>

检查路径是否存在。

参数类型
pathstring | URL
选项?ExistsOptions

Promise<boolean>

import { exists, BaseDirectory } from '@tauri-apps/plugin-fs';
// Check if the `$APPDATA/avatar.png` file exists
await exists('avatar.png', { baseDir: BaseDirectory.AppData });

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1149


function lstat(path, options?): Promise<FileInfo>

解析为指定 pathFileInfo。如果 path 是符号链接,则返回符号链接的信息而不是其指向的内容。

参数类型
pathstring | URL
选项?StatOptions

Promise<FileInfo>

import { lstat, BaseDirectory } from '@tauri-apps/plugin-fs';
const fileInfo = await lstat("hello.txt", { baseDir: BaseDirectory.AppLocalData });
console.log(fileInfo.isFile); // true

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L982


function mkdir(path, options?): Promise<void>

创建具有指定路径的新目录。

参数类型
pathstring | URL
选项?MkdirOptions

Promise<void>

import { mkdir, BaseDirectory } from '@tauri-apps/plugin-fs';
await mkdir('users', { baseDir: BaseDirectory.AppLocalData });

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L644


function open(path, options?): Promise<FileHandle>

打开文件并解析为 FileHandle 的实例。如果使用 createcreateNew 打开选项,则文件不需要事先存在。调用者有责任在文件使用完毕后关闭它。

参数类型
pathstring | URL
选项?OpenOptions

Promise<FileHandle>

import { open, BaseDirectory } from "@tauri-apps/plugin-fs"
const file = await open("foo/bar.txt", { read: true, write: true, baseDir: BaseDirectory.AppLocalData });
// Do work with file
await file.close();

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L565


function readDir(path, options?): Promise<DirEntry[]>

读取给定路径的目录并返回一个 DirEntry 数组。

参数类型
pathstring | URL
选项?ReadDirOptions

Promise<DirEntry[]>

import { readDir, BaseDirectory } from '@tauri-apps/plugin-fs';
import { join } from '@tauri-apps/api/path';
const dir = "users"
const entries = await readDir('users', { baseDir: BaseDirectory.AppLocalData });
processEntriesRecursively(dir, entries);
async function processEntriesRecursively(parent, entries) {
for (const entry of entries) {
console.log(`Entry: ${entry.name}`);
if (entry.isDirectory) {
const dir = await join(parent, entry.name);
processEntriesRecursively(dir, await readDir(dir, { baseDir: BaseDirectory.AppLocalData }))
}
}
}

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L706


function readFile(path, options?): Promise<Uint8Array>

读取文件所有内容并解析为字节数组。如果需要,可以使用 TextDecoder 将字节转换为字符串。

参数类型
pathstring | URL
选项?ReadFileOptions

Promise<Uint8Array>

import { readFile, BaseDirectory } from '@tauri-apps/plugin-fs';
const contents = await readFile('avatar.png', { baseDir: BaseDirectory.Resource });

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L739


function readTextFile(path, options?): Promise<string>

读取文件所有内容并返回 UTF-8 字符串。

参数类型
pathstring | URL
选项?ReadFileOptions

Promise<string>

import { readTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
const contents = await readTextFile('app.conf', { baseDir: BaseDirectory.AppConfig });

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L765


function readTextFileLines(path, options?): Promise<AsyncIterableIterator<string>>

返回一个异步 AsyncIterableIterator,以 UTF-8 字符串形式迭代文件的行。

参数类型
pathstring | URL
选项?ReadFileOptions

Promise<AsyncIterableIterator<string>>

import { readTextFileLines, BaseDirectory } from '@tauri-apps/plugin-fs';
const lines = await readTextFileLines('app.conf', { baseDir: BaseDirectory.AppConfig });
for await (const line of lines) {
console.log(line);
}

您还可以调用 AsyncIterableIterator.next 来推进迭代器,以便您可以在需要时懒惰地读取下一行。

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L798


function remove(path, options?): Promise<void>

删除指定的文件或目录。如果目录不为空且未将 recursive 选项设置为 true,则 promise 将被拒绝。

参数类型
pathstring | URL
选项?RemoveOptions

Promise<void>

import { remove, BaseDirectory } from '@tauri-apps/plugin-fs';
await remove('users/file.txt', { baseDir: BaseDirectory.AppLocalData });
await remove('users', { baseDir: BaseDirectory.AppLocalData });

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L877


function rename(
oldPath,
newPath,
options?): Promise<void>

将 oldpath 重命名(移动)到 newpath。路径可以是文件或目录。如果 newpath 已存在且不是目录,则 rename() 将替换它。当 oldpath 和 newpath 位于不同的目录中时,可能会应用操作系统特定的限制。

在 Unix 上,此操作不遵循任何路径上的符号链接。

参数类型
oldPathstring | URL
newPathstring | URL
选项?RenameOptions

Promise<void>

import { rename, BaseDirectory } from '@tauri-apps/plugin-fs';
await rename('avatar.png', 'deleted.png', { oldPathBaseDir: BaseDirectory.App, newPathBaseDir: BaseDirectory.AppLocalData });

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L916


function size(path): Promise<number>

获取文件或目录的大小。对于文件,也可以使用 stat 函数。

如果 path 是目录,此函数将递归迭代 path 内的每个文件和每个目录,因此如果用于较大的目录,将非常耗时。

参数类型
pathstring | URL

Promise<number>

import { size, BaseDirectory } from '@tauri-apps/plugin-fs';
// Get the size of the `$APPDATA/tauri` directory.
const dirSize = await size('tauri', { baseDir: BaseDirectory.AppData });
console.log(dirSize); // 1024

2.1.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1334


function stat(path, options?): Promise<FileInfo>

解析为指定 pathFileInfo。将始终遵循符号链接,但如果符号链接指向范围之外的路径,则会拒绝。

参数类型
pathstring | URL
选项?StatOptions

Promise<FileInfo>

import { stat, BaseDirectory } from '@tauri-apps/plugin-fs';
const fileInfo = await stat("hello.txt", { baseDir: BaseDirectory.AppLocalData });
console.log(fileInfo.isFile); // true

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L956


function truncate(
path,
len?,
options?): Promise<void>

截断或扩展指定文件,以达到指定的 len。如果 len0 或未指定,则整个文件内容将被截断。

参数类型
pathstring | URL
长度?数字
选项?TruncateOptions

Promise<void>

import { truncate, readTextFile, writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
// truncate the entire file
await truncate("my_file.txt", 0, { baseDir: BaseDirectory.AppLocalData });
// truncate part of the file
const filePath = "file.txt";
await writeTextFile(filePath, "Hello World", { baseDir: BaseDirectory.AppLocalData });
await truncate(filePath, 7, { baseDir: BaseDirectory.AppLocalData });
const data = await readTextFile(filePath, { baseDir: BaseDirectory.AppLocalData });
console.log(data); // "Hello W"

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1022


function watch(
paths,
cb,
options?): Promise<UnwatchFn>

观察文件或目录的变化(延迟后)。

参数类型
pathsstring | URL | string[] | URL[]
cb(event) => void
选项?DebouncedWatchOptions

Promise<UnwatchFn>

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1291


function watchImmediate(
paths,
cb,
options?): Promise<UnwatchFn>

观察文件或目录的变化。

参数类型
pathsstring | URL | string[] | URL[]
cb(event) => void
选项?WatchOptions

Promise<UnwatchFn>

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1308


function writeFile(
path,
data,
options?): Promise<void>

data 写入给定 path,默认情况下如果需要则创建新文件,否则覆盖。

参数类型
pathstring | URL
数据Uint8Array | ReadableStream<Uint8Array>
选项?WriteFileOptions

Promise<void>

import { writeFile, BaseDirectory } from '@tauri-apps/plugin-fs';
let encoder = new TextEncoder();
let data = encoder.encode("Hello World");
await writeFile('file.txt', data, { baseDir: BaseDirectory.AppLocalData });

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1067


function writeTextFile(
path,
data,
options?): Promise<void>

将 UTF-8 字符串 data 写入给定 path,默认情况下如果需要则创建新文件,否则覆盖。

参数类型
pathstring | URL
数据字符串
选项?WriteFileOptions

Promise<void>

import { writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
await writeTextFile('file.txt', "Hello world", { baseDir: BaseDirectory.AppLocalData });

2.0.0

来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1111


© 2025 Tauri 贡献者。CC-BY / MIT