跳到内容
Tauri

@tauri-apps/plugin-fs

访问文件系统。

安全

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

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

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

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

作用域也可以应用于特定的 fs API,方法是使用 API 的标识符而不是 fs:scope

{
"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 会导致承诺被拒绝,因为访问被拒绝。

枚举

BaseDirectory

2.0.0

枚举成员

AppCache
AppCache: 16;
参见

appCacheDir 了解更多信息。

来源: 未定义

AppConfig
AppConfig: 13;
参见

appConfigDir 了解更多信息。

来源: 未定义

AppData
AppData: 14;
参见

appDataDir 了解更多信息。

来源: 未定义

AppLocalData
AppLocalData: 15;
参见

appLocalDataDir 了解更多信息。

来源: 未定义

AppLog
AppLog: 17;
参见

appLogDir 了解更多信息。

来源: 未定义

Audio
Audio: 1;
参见

audioDir 了解更多信息。

来源: 未定义

Cache
Cache: 2;
参见

cacheDir 了解更多信息。

来源: 未定义

Config
Config: 3;
参见

configDir 了解更多信息。

来源: 未定义

Data
Data: 4;
参见

dataDir 了解更多信息。

来源: 未定义

Desktop
Desktop: 18;
参见

desktopDir 了解更多信息。

来源: 未定义

Document
Document: 6;
参见

documentDir 了解更多信息。

来源: 未定义

Download
Download: 7;
参见

downloadDir 了解更多信息。

来源: 未定义

Executable
Executable: 19;
参见

executableDir 了解更多信息。

来源: 未定义

Font
Font: 20;
参见

fontDir 了解更多信息。

来源: 未定义

Home
Home: 21;
参见

homeDir 了解更多信息。

来源: 未定义

LocalData
LocalData: 5;
参见

localDataDir 了解更多信息。

来源: 未定义

Picture
Picture: 8;
参见

pictureDir 了解更多信息。

来源: 未定义

Public
Public: 9;
参见

publicDir 了解更多信息。

来源: 未定义

Resource
Resource: 11;
参见

resourceDir 了解更多信息。

来源: 未定义

Runtime
Runtime: 22;
参见

runtimeDir 了解更多信息。

来源: 未定义

Temp
Temp: 12;
参见

tempDir 了解更多信息。

来源: 未定义

Template
Template: 23;
参见

templateDir 了解更多信息。

来源: 未定义

Video
Video: 10;
参见

videoDir 了解更多信息。

来源: 未定义


SeekMode

枚举成员

Current
Current: 1;

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

End
End: 2;

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

Start
Start: 0;

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

FileHandle

用于读取和写入文件的 Tauri 抽象。

2.0.0

继承自

  • Resource

构造函数

new FileHandle()
new FileHandle(rid): FileHandle
参数
参数类型
ridnumber
返回值

FileHandle

继承自

Resource.constructor

来源: 未定义

访问器

rid
获取签名
get rid(): number
返回值

number

继承自

Resource.rid

来源: 未定义

方法

close()
close(): Promise<void>

销毁并从内存中清理此资源。您不应再在此对象上调用任何方法,并且应删除对其的任何引用。

返回值

Promise<void>

继承自

Resource.close

来源: 未定义

read()
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 行为。

参数
参数类型
bufferUint8Array<ArrayBufferLike>
返回值

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()
seek(offset, whence): Promise<number>

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

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

参数
参数类型
offsetnumber
whenceSeekMode
返回值

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()
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()
truncate(len?): Promise<void>

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

参数
参数类型
len?number
返回值

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()
write(data): Promise<number>

data 向底层数据流写入 data.byteLength 字节。它解析为从 data 写入的字节数 (0 <= n <= data.byteLength),或者拒绝并返回导致写入提前停止的错误。write() 如果解析为 n < data.byteLength,则必须拒绝并返回非空错误。write() 不得修改切片数据,即使是暂时的。

参数
参数类型
dataUint8Array<ArrayBufferLike>
返回值

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

接口

CopyFileOptions

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

CreateOptions

2.0.0

属性

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

DebouncedWatchOptions

2.0.0

继承自

属性

属性类型描述继承自定义于
baseDir?BaseDirectorypath 的基本目录WatchOptions.baseDir来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1170
delayMs?number防抖延迟-来源: 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

DirEntry

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

这是 readDir 的结果。

2.0.0

属性

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

ExistsOptions

2.0.0

属性

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

FileInfo

FileInfo 描述文件,由 statlstatfstat 返回。

2.0.0

属性

属性类型描述定义于
atimenull | Date文件的上次访问时间。这对应于 Unix 上 statatime 字段和 Windows 上 ftLastAccessTime。这可能并非在所有平台上都可用。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L120
birthtimenull | Date文件的创建时间。这对应于 Mac/BSD 上 statbirthtime 字段和 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 | numberInode 编号。#### 平台特定 - Windows: 不支持。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L154
isDirectoryboolean如果此项是常规目录的信息,则为真。与 FileInfo.isFileFileInfo.isSymlink 互斥。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L99
isFileboolean如果此项是常规文件的信息,则为真。与 FileInfo.isDirectoryFileInfo.isSymlink 互斥。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L94
isSymlinkboolean如果此项是符号链接的信息,则为真。与 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 上 stat 中的 mtime 字段和 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
readonlyboolean此文件是否为只读(不可写)。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L128
sizenumber文件的大小,以字节为单位。来源: 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

MkdirOptions

2.0.0

属性

属性类型描述定义于
baseDir?BaseDirectorypath 的基本目录来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L631
mode?number创建目录时要使用的权限(默认为 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

OpenOptions

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?number如果创建文件,则使用的权限(默认为 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

ReadDirOptions

2.0.0

属性

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

ReadFileOptions

2.0.0

属性

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

RemoveOptions

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

RenameOptions

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

StatOptions

2.0.0

属性

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

TruncateOptions

2.0.0

属性

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

WatchEvent

2.0.0

属性

属性类型定义于
attrs未知来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1187
pathsstring[]来源: 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

WatchOptions

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

WriteFileOptions

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?number文件权限。在 Windows 上忽略。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1049

类型别名

UnwatchFn()

type UnwatchFn: () => void;

返回值

void

2.0.0

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


WatchEventKind

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


WatchEventKindAccess

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


WatchEventKindCreate

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


WatchEventKindModify

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


WatchEventKindRemove

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

函数

copyFile()

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

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

参数

参数类型
fromPathstring | URL
toPathstring | URL
options?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


create()

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

创建一个文件(如果不存在)或截断现有文件,并解析为 FileHandle 的实例。

参数

参数类型
pathstring | URL
options?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


exists()

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

检查路径是否存在。

参数

参数类型
pathstring | URL
options?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


lstat()

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

解析为指定 pathFileInfo。如果 path 是符号链接,将返回符号链接的信息,而不是它指向的目标。

参数

参数类型
pathstring | URL
options?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


mkdir()

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

使用指定的路径创建一个新目录。

参数

参数类型
pathstring | URL
options?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


open()

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

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

参数

参数类型
pathstring | URL
options?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


readDir()

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

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

参数

参数类型
pathstring | URL
options?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


readFile()

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

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

参数

参数类型
pathstring | URL
options?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


readTextFile()

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

读取文件并将其整个内容作为 UTF-8 字符串返回。

参数

参数类型
pathstring | URL
options?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


readTextFileLines()

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

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

参数

参数类型
pathstring | URL
options?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


remove()

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

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

参数

参数类型
pathstring | URL
options?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


rename()

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

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

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

参数

参数类型
oldPathstring | URL
newPathstring | URL
options?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


size()

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#L1348


stat()

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

解析为指定 pathFileInfo。将始终跟踪符号链接,但如果符号链接指向作用域外的路径,则将拒绝操作。

参数

参数类型
pathstring | URL
options?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


truncate()

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

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

参数

参数类型
pathstring | URL
len?number
options?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


watch()

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

监视文件或目录上的更改(延迟后)。

参数

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

返回值

Promise<UnwatchFn>

2.0.0

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


watchImmediate()

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

监视文件或目录上的更改。

参数

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

返回值

Promise<UnwatchFn>

2.0.0

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


writeFile()

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

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

参数

参数类型
pathstring | URL
dataUint8Array<ArrayBufferLike> | ReadableStream<Uint8Array<ArrayBufferLike>>
options?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


writeTextFile()

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

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

参数

参数类型
pathstring | URL
datastring
options?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 Contributors. CC-BY / MIT