跳到內容
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,將會導致 Promise 拒絕,因為存取被拒絕。

枚舉

BaseDirectory

2.0.0

枚舉成員

AppCache
AppCache: 16;

來源:未定義

AppConfig
AppConfig: 13;

來源:未定義

AppData
AppData: 14;

來源:未定義

AppLocalData
AppLocalData: 15;

來源:未定義

AppLog
AppLog: 17;

來源:未定義

Audio
Audio: 1;

來源:未定義

Cache
Cache: 2;

來源:未定義

Config
Config: 3;

來源:未定義

Data
Data: 4;

來源:未定義

Desktop
Desktop: 18;

來源:未定義

Document
Document: 6;

來源:未定義

Download
Download: 7;

來源:未定義

Executable
Executable: 19;

來源:未定義

Font
Font: 20;

來源:未定義

Home
Home: 21;

來源:未定義

LocalData
LocalData: 5;

來源:未定義

Picture
Picture: 8;

來源:未定義

Public
Public: 9;

來源:未定義

Resource
Resource: 11;

來源:未定義

Runtime
Runtime: 22;

來源:未定義

Temp
Temp: 12;

來源:未定義

Template
Template: 23;

來源:未定義

Video
Video: 10;

來源:未定義


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
參數
參數類型
rid數字
返回

FileHandle

繼承自

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 行為。

參數
參數類型
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 操作的行為取決於實作。它會傳回游標位置的數字。

參數
參數類型
offset數字
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?數字
返回

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>

datadata.byteLength 個位元組寫入到底層資料流。它會解析為從 data 寫入的位元組數(0 <= n <= data.byteLength),或拒絕並返回導致寫入提前停止的錯誤。write() 如果會解析為 n < data.byteLength,則必須拒絕並返回非 null 錯誤。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#L1162
delayMs?數字延遲防抖-來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1170
recursive?boolean遞迴監看目錄WatchOptions.recursive來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1160

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

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如果這是常規目錄的資訊,則為 True。與 FileInfo.isFileFileInfo.isSymlink 互斥。來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L99
isFileboolean如果這是常規檔案的資訊,則為 True。與 FileInfo.isDirectoryFileInfo.isSymlink 互斥。來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L94
isSymlinkboolean如果這是符號連結的資訊,則為 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
readonlyboolean這是否為唯讀(不可寫入)檔案。來源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

MkdirOptions

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

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 會被忽略。Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L538
mode?數字若要建立檔案,使用的權限(預設值為 0o666,在程序的 umask 之前)。在 Windows 上會被忽略。Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L544
read?boolean設定讀取權限的選項。當此選項為 true 時,表示檔案在開啟後應為可讀取的。Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L504
truncate?boolean設定截斷先前檔案的選項。若成功開啟檔案並設定此選項,則如果檔案已存在,將會截斷檔案至 0 大小。檔案必須以寫入權限開啟,truncate 才能運作。Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L525
write?boolean設定寫入權限的選項。當此選項為 true 時,表示檔案在開啟後應為可寫入的。如果檔案已存在,任何對其進行的寫入呼叫都會覆寫其內容,預設情況下不會截斷檔案。Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L511

ReadDirOptions

2.0.0

屬性

屬性類型描述定義於
baseDir?BaseDirectorypath 的基本目錄Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L663

ReadFileOptions

2.0.0

屬性

屬性類型描述定義於
baseDir?BaseDirectorypath 的基本目錄Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L725

RemoveOptions

2.0.0

屬性

屬性類型描述定義於
baseDir?BaseDirectorypath 的基本目錄Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L862
recursive?boolean預設值為 false。若設為 true,即使路徑是非空目錄也會被移除。Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L860

RenameOptions

2.0.0

屬性

屬性類型描述定義於
newPathBaseDir?BaseDirectory用於 newPath 的基礎目錄。Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L898
oldPathBaseDir?BaseDirectory用於 oldPath 的基礎目錄。Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L896

StatOptions

2.0.0

屬性

屬性類型描述定義於
baseDir?BaseDirectorypath 的基本目錄。Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L940

TruncateOptions

2.0.0

屬性

屬性類型描述定義於
baseDir?BaseDirectorypath 的基本目錄。Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L999

WatchEvent

2.0.0

屬性

屬性類型定義於
attrs未知Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1179
pathsstring[]Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1178
typeWatchEventKindSource: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1177

WatchOptions

2.0.0

擴展自

屬性

屬性類型描述定義於
baseDir?BaseDirectorypath 的基本目錄來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1162
recursive?boolean遞迴監看目錄來源https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1160

WriteFileOptions

2.0.0

屬性

屬性類型描述定義於
append?boolean預設值為 false。若設為 true,將會附加到檔案末尾,而不是覆寫先前的內容。Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1043
baseDir?BaseDirectorypath 的基本目錄Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1051
create?boolean設定選項以允許建立新檔案,如果指定路徑上尚不存在檔案(預設值為 true)。Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1045
createNew?boolean設定選項以建立新檔案,若檔案已存在則會失敗。Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1047
mode?數字檔案權限。在 Windows 上會被忽略。Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1049

類型別名

UnwatchFn()

type UnwatchFn: () => void;

返回

void

2.0.0

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1243


WatchEventKind

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

2.0.0

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1185


WatchEventKindAccess

type WatchEventKindAccess: object | object | object | object;

2.0.0

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


WatchEventKindCreate

type WatchEventKindCreate: object | object | object | object;

2.0.0

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


WatchEventKindModify

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

2.0.0

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1214


WatchEventKindRemove

type WatchEventKindRemove: object | object | object | object;

2.0.0

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

函數

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

Source: 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

Source: 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

Source: 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 是符號連結,則會傳回符號連結的資訊,而不是它指向的目標。

參數

參數類型
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

Source: 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

Source: 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

Source: 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

Source: 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

Source: 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

Source: 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

Source: 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

Source: 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

Source: 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

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1340


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

Source: 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?數字
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

Source: 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

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


watchImmediate()

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

監看檔案或目錄的變更。

參數

參數類型
pathsstring | URL | string[] | URL[]
cb(event) => void
options?WatchOptions

返回

Promise<UnwatchFn>

2.0.0

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


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

Source: 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

Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1103


© 2025 Tauri Contributors. CC-BY / MIT