fs
存取檔案系統。
當 tauri.conf.json
中的 build.withGlobalTauri
設為 true
時,也可以使用 window.__TAURI__.fs
存取此套件。
必須將 API 新增到 tauri.conf.json
中的 tauri.allowlist.fs
{
"tauri": {
"allowlist": {
"fs": {
"all": true, // enable all FS APIs
"readFile": true,
"writeFile": true,
"readDir": true,
"copyFile": true,
"createDir": true,
"removeDir": true,
"removeFile": true,
"renameFile": true,
"exists": true
}
}
}
}
建議僅允許您使用的 API,以取得最佳的套件大小和安全性。
安全性
此模組會防止路徑穿越,不允許絕對路徑或父目錄元件(即不允許 "/usr/path/to/file" 或 "../path/to/file" 路徑)。使用此 API 存取的路徑必須相對於其中一個基本目錄,因此如果您需要存取任意檔案系統路徑,您必須改寫核心層的此類邏輯。
此 API 具有範圍設定,強制您限制可以使用 glob 模式存取的路徑。
範圍設定是一個描述允許資料夾路徑的 glob 模式陣列。例如,此範圍設定僅允許存取$APPDATA 目錄中databases 資料夾中的檔案。
{
"tauri": {
"allowlist": {
"fs": {
"scope": ["$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
、$APP
、$LOG
、$TEMP
。
嘗試執行範圍中未設定 URL 的任何 API 都會導致承諾拒絕,因為存取遭到拒絕。
請注意,此範圍適用於此模組上的所有 API。
參考
Dir
重新命名並重新匯出 BaseDirectory
writeFile
重新命名並重新匯出 writeTextFile
列舉
BaseDirectory
自: 1.0.0
列舉成員
介面
FileEntry
自: 1.0.0
屬性
children
Optional
children:FileEntry
[]
此條目的子項(如果是目錄);否則為 null
定義於: fs.ts:167
name
Optional
name:string
目錄/檔案的名稱,如果路徑以 ..
結尾,則可能為 null
定義於: fs.ts:165
path
path:
string
定義於: fs.ts:160
FsBinaryFileOption
用於將二進位資料寫入檔案的選項物件。
自: 1.0.0
屬性
contents
contents:
BinaryFileContents
位元組陣列內容。
定義於: fs.ts:153
path
path:
string
要寫入檔案的路徑。
定義於: fs.ts:151
FsDirOptions
自: 1.0.0
屬性
dir
Optional
dir:BaseDirectory
定義於: fs.ts:126
recursive
Optional
recursive:boolean
定義於: fs.ts:127
FsOptions
自: 1.0.0
屬性
append
Optional
append:boolean
內容是否應覆寫檔案內容或附加到檔案內容。
自: 1.5.0
定義於: fs.ts:118
dir
Optional
dir:BaseDirectory
定義於: fs.ts:112
FsTextFileOption
用於將 UTF-8 字串寫入檔案的選項物件。
自: 1.0.0
屬性
contents
contents:
string
要寫入檔案的 UTF-8 字串。
定義於: fs.ts:139
path
path:
string
要寫入檔案的路徑。
定義於: fs.ts:137
類型別名
BinaryFileContents
BinaryFileContents:
Iterable
<number
> |ArrayLike
<number
> |ArrayBuffer
定義於: fs.ts:142
函式
copyFile
copyFile(
source
:字串
,destination
:字串
,options?
:FsOptions
):Promise
<void
>
將檔案複製到目的地。
範例
import { copyFile, BaseDirectory } from '@tauri-apps/api/fs';
// Copy the `$APPCONFIG/app.conf` file to `$APPCONFIG/app.conf.bk`
await copyFile('app.conf', 'app.conf.bk', { dir: BaseDirectory.AppConfig });
自: 1.0.0
參數
名稱 | 類型 |
---|---|
source | 字串 |
destination | 字串 |
options | FsOptions |
傳回: Promise
<void
>
表示操作成功或失敗的承諾。
createDir
createDir(
dir
:字串
,options?
:FsDirOptions
):Promise
<void
>
建立目錄。如果路徑的其中一個父元件不存在,且 recursive
選項未設為 true,承諾會被拒絕。
範例
import { createDir, BaseDirectory } from '@tauri-apps/api/fs';
// Create the `$APPDATA/users` directory
await createDir('users', { dir: BaseDirectory.AppData, recursive: true });
自: 1.0.0
參數
名稱 | 類型 |
---|---|
dir | 字串 |
options | FsDirOptions |
傳回: Promise
<void
>
表示操作成功或失敗的承諾。
exists
檢查路徑是否存在。
範例
import { exists, BaseDirectory } from '@tauri-apps/api/fs';
// Check if the `$APPDATA/avatar.png` file exists
await exists('avatar.png', { dir: BaseDirectory.AppData });
自: 1.1.0
參數
名稱 | 類型 |
---|---|
path | 字串 |
options | FsOptions |
傳回: Promise
<布林值
>
readBinaryFile
readBinaryFile(
filePath
:string
,options?
:FsOptions
):Promise
<Uint8Array
>
以位元組陣列形式讀取檔案。
範例
import { readBinaryFile, BaseDirectory } from '@tauri-apps/api/fs';
// Read the image file in the `$RESOURCEDIR/avatar.png` path
const contents = await readBinaryFile('avatar.png', { dir: BaseDirectory.Resource });
自: 1.0.0
參數
名稱 | 類型 |
---|---|
filePath | 字串 |
options | FsOptions |
傳回: Promise
<Uint8Array
>
readDir
readDir(
dir
:string
,options?
:FsDirOptions
):Promise
<FileEntry
[]>
列出目錄檔案。
範例
import { readDir, BaseDirectory } from '@tauri-apps/api/fs';
// Reads the `$APPDATA/users` directory recursively
const entries = await readDir('users', { dir: BaseDirectory.AppData, recursive: true });
function processEntries(entries) {
for (const entry of entries) {
console.log(`Entry: ${entry.path}`);
if (entry.children) {
processEntries(entry.children)
}
}
}
自: 1.0.0
參數
名稱 | 類型 |
---|---|
dir | 字串 |
options | FsDirOptions |
readTextFile
readTextFile(
filePath
:string
,options?
:FsOptions
):Promise
<string
>
以 UTF-8 編碼字串形式讀取檔案。
範例
import { readTextFile, BaseDirectory } from '@tauri-apps/api/fs';
// Read the text file in the `$APPCONFIG/app.conf` path
const contents = await readTextFile('app.conf', { dir: BaseDirectory.AppConfig });
自: 1.0.0
參數
名稱 | 類型 |
---|---|
filePath | 字串 |
options | FsOptions |
傳回: Promise
<string
>
removeDir
removeDir(
dir
:string
,options?
:FsDirOptions
):Promise
<void
>
移除目錄。如果目錄不為空,且未將 recursive
選項設定為 true,則承諾會被拒絕。
範例
import { removeDir, BaseDirectory } from '@tauri-apps/api/fs';
// Remove the directory `$APPDATA/users`
await removeDir('users', { dir: BaseDirectory.AppData });
自: 1.0.0
參數
名稱 | 類型 |
---|---|
dir | 字串 |
options | FsDirOptions |
傳回: Promise
<void
>
表示操作成功或失敗的承諾。
removeFile
removeFile(
file
:string
,options?
:FsOptions
):Promise
<void
>
移除檔案。
範例
import { removeFile, BaseDirectory } from '@tauri-apps/api/fs';
// Remove the `$APPConfig/app.conf` file
await removeFile('app.conf', { dir: BaseDirectory.AppConfig });
自: 1.0.0
參數
名稱 | 類型 |
---|---|
檔案 | 字串 |
options | FsOptions |
傳回: Promise
<void
>
表示操作成功或失敗的承諾。
renameFile
renameFile(
oldPath
:string
,newPath
:string
,options?
:FsOptions
):Promise
<void
>
重新命名檔案。
範例
import { renameFile, BaseDirectory } from '@tauri-apps/api/fs';
// Rename the `$APPDATA/avatar.png` file
await renameFile('avatar.png', 'deleted.png', { dir: BaseDirectory.AppData });
自: 1.0.0
參數
名稱 | 類型 |
---|---|
舊路徑 | 字串 |
新路徑 | 字串 |
options | FsOptions |
傳回: Promise
<void
>
表示操作成功或失敗的承諾。
writeBinaryFile
writeBinaryFile(
path
:string
,contents
:BinaryFileContents
,options?
:FsOptions
):Promise
<void
>
將位元組陣列內容寫入檔案。
範例
import { writeBinaryFile, BaseDirectory } from '@tauri-apps/api/fs';
// Write a binary file to the `$APPDATA/avatar.png` path
await writeBinaryFile('avatar.png', new Uint8Array([]), { dir: BaseDirectory.AppData });
自: 1.0.0
參數
名稱 | 類型 | 說明 |
---|---|---|
path | 字串 | - |
內容 | BinaryFileContents | - |
選項? | FsOptions | 設定物件。 |
傳回: Promise
<void
>
表示操作成功或失敗的承諾。
writeBinaryFile(
file
:FsBinaryFileOption
,options?
:FsOptions
):Promise
<void
>
將位元組陣列內容寫入檔案。
範例
import { writeBinaryFile, BaseDirectory } from '@tauri-apps/api/fs';
// Write a binary file to the `$APPDATA/avatar.png` path
await writeBinaryFile({ path: 'avatar.png', contents: new Uint8Array([]) }, { dir: BaseDirectory.AppData });
自: 1.0.0
參數
名稱 | 類型 | 說明 |
---|---|---|
檔案 | FsBinaryFileOption | 包含檔案路徑和內容的物件。 |
選項? | FsOptions | 設定物件。 |
傳回: Promise
<void
>
表示操作成功或失敗的承諾。
writeTextFile
writeTextFile(
path
:string
,contents
:string
,options?
:FsOptions
):Promise
<void
>
撰寫 UTF-8 文字檔。
範例
import { writeTextFile, BaseDirectory } from '@tauri-apps/api/fs';
// Write a text file to the `$APPCONFIG/app.conf` path
await writeTextFile('app.conf', 'file contents', { dir: BaseDirectory.AppConfig });
自: 1.0.0
參數
名稱 | 類型 |
---|---|
path | 字串 |
內容 | 字串 |
選項? | FsOptions |
傳回: Promise
<void
>
writeTextFile(
file
:FsTextFileOption
,options?
:FsOptions
):Promise
<void
>
撰寫 UTF-8 文字檔。
範例
import { writeTextFile, BaseDirectory } from '@tauri-apps/api/fs';
// Write a text file to the `$APPCONFIG/app.conf` path
await writeTextFile({ path: 'app.conf', contents: 'file contents' }, { dir: BaseDirectory.AppConfig });
自: 1.0.0
參數
名稱 | 類型 |
---|---|
檔案 | FsTextFileOption |
選項? | FsOptions |
傳回: Promise
<void
>
表示操作成功或失敗的承諾。