跳转至内容
Tauri

@tauri-apps/plugin-fs

访问文件系统。

安全

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

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

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

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

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

{
"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;

出处: 未定义

应用程序配置
AppConfig: 13;

出处: 未定义

应用程序数据
AppData: 14;

出处: 未定义

应用程序本地数据
AppLocalData: 15;

出处: 未定义

应用程序日志
AppLog: 17;

出处: 未定义

音频
Audio: 1;

出处: 未定义

缓存
Cache: 2;

出处: 未定义

配置
Config: 3;

出处: 未定义

数据
Data: 4;

出处: 未定义

桌面
Desktop: 18;

出处: 未定义

文档
Document: 6;

出处: 未定义

下载
Download: 7;

出处: 未定义

可执行文件
Executable: 19;

出处: 未定义

字体
Font: 20;

出处: 未定义

主页
Home: 21;

出处: 未定义

本地数据
LocalData: 5;

出处: 未定义

图片
Picture: 8;

出处: 未定义

公共
Public: 9;

出处: 未定义

资源
Resource: 11;

出处: 未定义

运行时
Runtime: 22;

出处: 未定义

临时
Temp: 12;

出处: 未定义

模板
Template: 23;

出处: 未定义

视频
Video: 10;

出处: 未定义


搜索模式

枚举成员

当前
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

扩展

  • 资源

构造函数

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

文件句柄

继承自

Resource.constructor

出处: 未定义

访问器

rid
获取签名
get rid(): 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行为。

参数
参数类型
缓冲区Uint8Array<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 函数设置 next read() 或 write() 操作的偏移量到 offset,根据 whence 的解释:Start 表示相对于文件开头,Current 表示相对于当前位置,End 表示相对于文件末尾。Seek 函数将返回相对于文件开头的新的偏移量。

尝试将偏移量设置为文件开始之前是一个错误。将偏移量设置到任何正值是合法的,但底层对象后续 I/O 操作的行为依赖于具体实现。它返回光标的当前位置。

参数
参数类型
offset数字
whence搜索模式
返回

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?数字
返回

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

接口

复制文件选项

自从

2.0.0

属性

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

创建选项

自从

2.0.0

属性

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

去抖动监视选项

自从

2.0.0

扩展

属性

属性类型描述继承自
baseDir?基本目录path 的基本目录WatchOptions.baseDir来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1162
delayMs?数字防抖延迟-来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1170
recursive?布尔值递归监视目录观察选项.recursive来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1160

目录条目

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

这是由readDir的结果。

自从

2.0.0

属性

属性类型描述
isDirectory布尔值指定此条目是否为目录。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L677
isFile布尔值指定此条目是否为文件。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L679
isSymlink布尔值指定此条目是否为符号链接。来源: 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?基本目录基础目录用于path来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1127

文件信息

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
isDirectory布尔值如果是常规目录的信息,则为 True。与 FileInfo.isFileFileInfo.isSymlink 互斥。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L99
isFile布尔值如果是常规文件的信息,则为 True。与 FileInfo.isDirectoryFileInfo.isSymlink 互斥。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L94
isSymlink布尔值如果是符号链接的信息,则为 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 中与 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
readonly布尔值是否为只读(不可写)文件。来源: 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?基本目录path 的基本目录来源: 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?布尔值默认为 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?布尔值设置附加模式的选项。当此选项为 true 时,表示写入将追加到文件中,而不是覆盖前面的内容。请注意,设置 { write: true, append: true } 与仅设置 { append: true } 具有相同的效果。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L518
baseDir?基本目录path 的基本目录来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L546
create?布尔值设置选项以允许在指定的路径不存在文件时创建新文件。需要使用或追加访问。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L531
createNew?布尔值默认为 false。如果设置为 true,则不允许在目标位置存在文件、目录或符号链接。需要使用或追加访问。当 createNew 设置为 true 时,创建和截断被忽略。来源: 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?布尔值设置读取访问的选项。当此选项为 true 时,表示文件如果打开,应该可读。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L504
truncate?布尔值设置截断旧文件的选项。如果设置此选项成功打开文件,则将文件截断到 0 大小(如果它已存在)。文件必须具有写入访问权限才能截断。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L525
write?布尔值设置写入访问的选项。当此选项为 true 时,表示如果打开文件,则应该可写。如果文件已存在,任何对此文件的写入调用将覆盖其内容,默认情况下不进行截断。来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L511

读取目录选项

自从

2.0.0

属性

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

读取文件选项

自从

2.0.0

属性

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

删除选项

自从

2.0.0

属性

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

重命名选项

自从

2.0.0

属性

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

状态选项

自从

2.0.0

属性

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

截断选项

自从

2.0.0

属性

属性类型描述
baseDir?基本目录基础目录用于path来源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#L1179
paths字符串[]来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1178
type监视事件类型来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1177

监视选项

自从

2.0.0

扩展由

属性

属性类型描述
baseDir?基本目录path 的基本目录来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1162
recursive?布尔值递归监视目录来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1160

写文件选项

自从

2.0.0

属性

属性类型描述
append?布尔值默认值 false。如果设置为 true,将追加到文件中而不是覆盖以前的文件内容。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1043
baseDir?基本目录path 的基本目录来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1051
create?布尔值设置选项以允许在指定的路径上创建新文件(默认值为 true)。来源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1045
createNew?布尔值设置选项以创建新文件,如果它已存在则失败。来源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#L1243


监视事件类型

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


监视事件类型访问

type WatchEventKindAccess: object | object | object | object;

自从

2.0.0

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


监视事件类型创建

type WatchEventKindCreate: object | object | object | object;

自从

2.0.0

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


监视事件类型修改

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


监视事件类型删除

type WatchEventKindRemove: object | object | object | object;

自从

2.0.0

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

函数

copyFile()

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

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

参数

参数类型
fromPath字符串 | URL
toPath字符串 | URL
options?复制文件选项

返回

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 实例。

参数

参数类型
路径字符串 | URL
options?创建选项

返回

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>

检查路径是否存在。

参数

参数类型
路径字符串 | URL
options?存在选项

返回

Promise<布尔值>

示例

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


lstat()

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

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

参数

参数类型
路径字符串 | URL
options?状态选项

返回

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>

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

参数

参数类型
路径字符串 | URL
options?创建目录选项

返回

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 打开选项,文件不需要先前存在。文件打开后完成时,关闭文件的调用方负责。

参数

参数类型
路径字符串 | URL
options?打开选项

返回

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 数组。

参数

参数类型
路径字符串 | URL
options?读取目录选项

返回

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 将字节转换为字符串。

参数

参数类型
路径字符串 | URL
options?读取文件选项

返回

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 字符串。

参数

参数类型
路径字符串 | URL
options?读取文件选项

返回

Promise<字符串>

示例

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 字符串行。

参数

参数类型
路径字符串 | URL
options?读取文件选项

返回

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 来 advancing the iterator 以便您可以随时延迟地读取下一行。

自从

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 将被拒绝。

参数

参数类型
路径字符串 | URL
options?删除选项

返回

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>

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

在 Unix 上,此操作在任一路径上都不会跟随符号链接。

参数

参数类型
oldPath字符串 | URL
newPath字符串 | URL
options?重命名选项

返回

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 内的每个文件和每个目录中进行迭代,因此在较大目录上使用时可能会导致时间非常耗费。

参数

参数类型
路径字符串 | 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#L1340


stat()

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

解析特定 path 的 FileInfo。始终跟随符号链接,但如果符号链接指向作用域之外的路由,则会拒绝。

参数

参数类型
路径字符串 | URL
options?状态选项

返回

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。如果 len 为 0 或未指定,则截断整个文件内容。

参数

参数类型
路径字符串 | URL
len?数字
options?截断选项

返回

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
options?去抖动监视选项

返回

Promise<UnwatchFn>

自从

2.0.0

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


即时监视()

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

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

参数

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

返回

Promise<UnwatchFn>

自从

2.0.0

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


writeFile()

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

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

参数

参数类型
路径字符串 | URL
dataUint8Array<ArrayBufferLike> | ReadableStream<Uint8Array<ArrayBufferLike>>
options?写文件选项

返回

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,默认情况下如果需要,则创建新文件,否则覆盖。

参数

参数类型
路径字符串 | URL
data字符串
options?写文件选项

返回

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


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