Skip to content
Tauri
Releases

fs

Access the file system.

This package is also accessible with window.__TAURI__.fs when build.withGlobalTauri in tauri.conf.json is set to true.

The APIs must be added to tauri.allowlist.fs in tauri.conf.json:

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

It is recommended to allowlist only the APIs you use for optimal bundle size and security.

Security

This module prevents path traversal, not allowing absolute paths or parent dir components (i.e. “/usr/path/to/file” or “../path/to/file” paths are not allowed). Paths accessed with this API must be relative to one of the base directories so if you need access to arbitrary filesystem paths, you must write such logic on the core layer instead.

The API has a scope configuration that forces you to restrict the paths that can be accessed using glob patterns.

The scope configuration is an array of glob patterns describing folder paths that are allowed. For instance, this scope configuration only allows accessing files on the databases folder of the $APPDATA directory:

{
"tauri": {
"allowlist": {
"fs": {
"scope": ["$APPDATA/databases/*"]
}
}
}
}

Notice the use of the $APPDATA variable. The value is injected at runtime, resolving to the app data directory. The available variables are: $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.

Trying to execute any API with a URL not configured on the scope results in a promise rejection due to denied access.

Note that this scope applies to all APIs on this module.

References

Dir

Renames and re-exports BaseDirectory

Dir;

Source: fs.ts:599


writeFile

Renames and re-exports writeTextFile

writeFile;

Source: fs.ts:603

Enumerations

BaseDirectory

Since

1.0.0

Enumeration Members

App
App: 18;

Source: fs.ts:98


AppCache
AppCache: 24;

Source: fs.ts:104


AppConfig
AppConfig: 21;

Source: fs.ts:101


AppData
AppData: 22;

Source: fs.ts:102


AppLocalData
AppLocalData: 23;

Source: fs.ts:103


AppLog
AppLog: 25;

Source: fs.ts:105


Audio
Audio: 1;

Source: fs.ts:81


Cache
Cache: 2;

Source: fs.ts:82


Config
Config: 3;

Source: fs.ts:83


Data
Data: 4;

Source: fs.ts:84


Desktop
Desktop: 6;

Source: fs.ts:86


Document
Document: 7;

Source: fs.ts:87


Download
Download: 8;

Source: fs.ts:88


Executable
Executable: 9;

Source: fs.ts:89


Font
Font: 10;

Source: fs.ts:90


Home
Home: 11;

Source: fs.ts:91


LocalData
LocalData: 5;

Source: fs.ts:85


Log
Log: 19;

Source: fs.ts:99


Picture
Picture: 12;

Source: fs.ts:92


Public
Public: 13;

Source: fs.ts:93


Resource
Resource: 17;

Source: fs.ts:97


Runtime
Runtime: 14;

Source: fs.ts:94


Temp
Temp: 20;

Source: fs.ts:100


Template
Template: 15;

Source: fs.ts:95


Video
Video: 16;

Source: fs.ts:96

Interfaces

FileEntry

Since

1.0.0

Properties

PropertyTypeDescription
children?FileEntry[]Children of this entry if it’s a directory; null otherwise
name?stringName of the directory/file
can be null if the path terminates with ..
pathstring-

FsBinaryFileOption

Options object used to write a binary data to a file.

Since

1.0.0

Properties

PropertyTypeDescription
contentsBinaryFileContentsThe byte array contents.
pathstringPath to the file to write.

FsDirOptions

Since

1.0.0

Properties

PropertyType
dir?BaseDirectory
recursive?boolean

FsOptions

Since

1.0.0

Properties

PropertyTypeDescription
append?booleanWhether the content should overwrite the content of the file or append to it.

Since

1.5.0
dir?BaseDirectory-

FsTextFileOption

Options object used to write a UTF-8 string to a file.

Since

1.0.0

Properties

PropertyTypeDescription
contentsstringThe UTF-8 string to write to the file.
pathstringPath to the file to write.

Type Aliases

BinaryFileContents

BinaryFileContents: Iterable<number> | ArrayLike<number> | ArrayBuffer;

Source: fs.ts:142

Functions

copyFile()

copyFile(
source,
destination,
options = {}): Promise< void >

Copies a file to a destination.

Example

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 });

Since

1.0.0

Parameters

ParameterType
sourcestring
destinationstring
optionsFsOptions

Returns

Promise< void >

A promise indicating the success or failure of the operation.

Source: fs.ts:495


createDir()

createDir(dir, options = {}): Promise< void >

Creates a directory. If one of the path’s parent components doesn’t exist and the recursive option isn’t set to true, the promise will be rejected.

Example

import { createDir, BaseDirectory } from '@tauri-apps/api/fs';
// Create the `$APPDATA/users` directory
await createDir('users', { dir: BaseDirectory.AppData, recursive: true });

Since

1.0.0

Parameters

ParameterType
dirstring
optionsFsDirOptions

Returns

Promise< void >

A promise indicating the success or failure of the operation.

Source: fs.ts:440


exists()

exists(path, options = {}): Promise< boolean >

Check if a path exists.

Example

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

Since

1.1.0

Parameters

ParameterType
pathstring
optionsFsOptions

Returns

Promise< boolean >

Source: fs.ts:578


readBinaryFile()

readBinaryFile(filePath, options = {}): Promise< Uint8Array >

Reads a file as byte array.

Example

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 });

Since

1.0.0

Parameters

ParameterType
filePathstring
optionsFsOptions

Returns

Promise< Uint8Array >

Source: fs.ts:206


readDir()

readDir(dir, options = {}): Promise< FileEntry[] >

List directory files.

Example

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);
}
}
}

Since

1.0.0

Parameters

ParameterType
dirstring
optionsFsDirOptions

Returns

Promise< FileEntry[] >

Source: fs.ts:411


readTextFile()

readTextFile(filePath, options = {}): Promise< string >

Reads a file as an UTF-8 encoded string.

Example

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 });

Since

1.0.0

Parameters

ParameterType
filePathstring
optionsFsOptions

Returns

Promise< string >

Source: fs.ts:181


removeDir()

removeDir(dir, options = {}): Promise< void >

Removes a directory. If the directory is not empty and the recursive option isn’t set to true, the promise will be rejected.

Example

import { removeDir, BaseDirectory } from '@tauri-apps/api/fs';
// Remove the directory `$APPDATA/users`
await removeDir('users', { dir: BaseDirectory.AppData });

Since

1.0.0

Parameters

ParameterType
dirstring
optionsFsDirOptions

Returns

Promise< void >

A promise indicating the success or failure of the operation.

Source: fs.ts:468


removeFile()

removeFile(file, options = {}): Promise< void >

Removes a file.

Example

import { removeFile, BaseDirectory } from '@tauri-apps/api/fs';
// Remove the `$APPConfig/app.conf` file
await removeFile('app.conf', { dir: BaseDirectory.AppConfig });

Since

1.0.0

Parameters

ParameterType
filestring
optionsFsOptions

Returns

Promise< void >

A promise indicating the success or failure of the operation.

Source: fs.ts:524


renameFile()

renameFile(
oldPath,
newPath,
options = {}): Promise< void >

Renames a file.

Example

import { renameFile, BaseDirectory } from '@tauri-apps/api/fs';
// Rename the `$APPDATA/avatar.png` file
await renameFile('avatar.png', 'deleted.png', { dir: BaseDirectory.AppData });

Since

1.0.0

Parameters

ParameterType
oldPathstring
newPathstring
optionsFsOptions

Returns

Promise< void >

A promise indicating the success or failure of the operation.

Source: fs.ts:551


writeBinaryFile()

writeBinaryFile(
path,
contents,
options?): Promise< void >

Writes a byte array content to a file.

Example

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 });

Since

1.0.0

Parameters

ParameterTypeDescription
pathstring-
contentsBinaryFileContents-
options?FsOptionsConfiguration object.

Returns

Promise< void >

A promise indicating the success or failure of the operation.

Source: fs.ts:315

writeBinaryFile(file, options?): Promise< void >

Writes a byte array content to a file.

Example

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 }
);

Since

1.0.0

Parameters

ParameterTypeDescription
fileFsBinaryFileOptionThe object containing the file path and contents.
options?FsOptionsConfiguration object.

Returns

Promise< void >

A promise indicating the success or failure of the operation.

Source: fs.ts:336


writeTextFile()

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

Writes a UTF-8 text file.

Example

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 });

Since

1.0.0

Parameters

ParameterType
pathstring
contentsstring
options?FsOptions

Returns

Promise< void >

Source: fs.ts:233

writeTextFile(file, options?): Promise< void >

Writes a UTF-8 text file.

Example

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 }
);

Since

1.0.0

Parameters

ParameterType
fileFsTextFileOption
options?FsOptions

Returns

Promise< void >

A promise indicating the success or failure of the operation.

Source: fs.ts:251


© 2024 Tauri Contributors. CC-BY / MIT