convertFileSrc for WebView file access in Tauri 2.0
For directly reading files from the filesystem into the WebView, Tauri 2.0 recommends using the convertFileSrc functionality, as it is most likely still faster if you do not need to process the data on the Rust backend.
fs plugin path traversal security prevention
The fs plugin prevents path traversal attacks by not allowing parent directory accessors. Paths like '/usr/path/to/../file' or '../path/to/file' are not allowed. Paths must be either relative to a base directory or created with the path API.
fs plugin base directory usage in JavaScript
Every fs API call accepts a baseDir option that acts as the working directory. For example, await readFile('avatars/tauri.png', { baseDir: BaseDirectory.Home }) reads ~/avatars/tauri.png.
fs plugin file create operation
The create() function creates a file and returns a handle to it. If the file already exists, it is truncated. Must call file.close() when done. Example: const file = await create('foo/bar.txt', { baseDir: BaseDirectory.AppData }); await file.write(...); await file.close();
fs plugin text file write operation
Use writeTextFile() for text files. Example: await writeTextFile('config.json', JSON.stringify({ notifications: true }), { baseDir: BaseDirectory.AppConfig });
fs plugin binary file write operation
Use writeFile() for binary files with Uint8Array. Example: const contents = new Uint8Array(); await writeFile('config', contents, { baseDir: BaseDirectory.AppConfig });
fs plugin file open modes
The open() function supports these modes: read (default, read-only), write (write-only), append (append instead of overwrite), truncate (truncate to 0 on write), create (create if not exists), createNew (fail if exists).
fs plugin read-only file open example
Example: const file = await open('foo/bar.txt', { read: true, baseDir: BaseDirectory.AppData }); const stat = await file.stat(); const buf = new Uint8Array(stat.size); await file.read(buf); const textContents = new TextDecoder().decode(buf); await file.close();
fs plugin append mode file operation
To append to a file, use open with append: true. Example: const file = await open('foo/bar.txt', { append: true, baseDir: BaseDirectory.AppData }); await file.write(new TextEncoder().encode('world')); await file.close();
fs plugin truncate option behavior
When truncate option is set to true and write is also true, the file is truncated to length 0 if it already exists. This option requires write to be true.
fs plugin create option for files
The create option (when set to true) creates the file if it does not exist, or opens it if it does. Requires write or append to be true.
fs plugin createNew option for files
The createNew option works like create but will fail if the file already exists. Requires write to be true.
fs plugin read text file operation
Use readTextFile() to read text files. Example: const configToml = await readTextFile('config.toml', { baseDir: BaseDirectory.AppConfig });
fs plugin read text file lines streaming
For large text files, use readTextFileLines() to stream lines. Example: const lines = await readTextFileLines('app.logs', { baseDir: BaseDirectory.AppLog }); for await (const line of lines) { console.log(line); }
fs plugin read binary file operation
Use readFile() to read binary files. Example: const icon = await readFile('icon.png', { baseDir: BaseDirectory.Resources });
fs plugin remove file operation
Use remove() to delete a file. If the file does not exist, an error is returned. Example: await remove('user.db', { baseDir: BaseDirectory.AppLocalData });
fs plugin copy file operation
Use copyFile() to copy files from source to destination. Both fromPathBaseDir and toPathBaseDir must be configured separately. Example: await copyFile('user.db', 'user.db.bk', { fromPathBaseDir: BaseDirectory.AppLocalData, toPathBaseDir: BaseDirectory.Temp });
fs plugin exists check operation
Use exists() to check if a file exists. Example: const tokenExists = await exists('token', { baseDir: BaseDirectory.AppLocalData });
fs plugin stat and lstat metadata operations
Use stat() to get file metadata following symlinks, or lstat() to get symlink information without following it. Example: const metadata = await stat('app.db', { baseDir: BaseDirectory.AppLocalData });
fs plugin rename file operation
Use rename() to rename files. Both fromPathBaseDir and toPathBaseDir must be configured separately. Example: await rename('user.db.bk', 'user.db', { fromPathBaseDir: BaseDirectory.AppLocalData, toPathBaseDir: BaseDirectory.Temp });
fs plugin truncate file operation
Use truncate() to truncate or extend a file to a specified length (defaults to 0). Example: await truncate('my_file.txt', 0, { baseDir: BaseDirectory.AppLocalData });
fs plugin mkdir directory creation
Use mkdir() to create a directory. Example: await mkdir('images', { baseDir: BaseDirectory.AppLocalData });
fs plugin readDir directory listing
Use readDir() to recursively list directory entries. Example: const entries = await readDir('users', { baseDir: BaseDirectory.AppLocalData });
fs plugin remove directory operation
Use remove() to delete a directory. If not empty, set recursive option to true. Example: await remove('images', { baseDir: BaseDirectory.AppLocalData, recursive: true });
fs plugin watch file changes with debounce
Use watch() to monitor file/directory changes with a debounce delay. Example: await watch('app.log', (event) => { console.log('event', event); }, { baseDir: BaseDirectory.AppLog, delayMs: 500 });
fs plugin watchImmediate file changes
Use watchImmediate() to immediately notify of file/directory changes without debounce. Example: await watchImmediate('logs', (event) => { console.log('event', event); }, { baseDir: BaseDirectory.AppLog, recursive: true });
fs plugin watch feature flag requirement
The watch and watchImmediate functions require the 'watch' feature flag in Cargo.toml: [dependencies] tauri-plugin-fs = { version = '2.0.0', features = ['watch'] }
fs plugin watch recursive option
By default, watch operations on a directory are not recursive. Set the recursive option to true to recursively watch for changes on all sub-directories.
fs plugin Rust side file manipulation recommendation
For file manipulation through Rust, use traditional Rust libraries: std::fs for synchronous operations or tokio::fs for asynchronous operations, rather than the fs plugin API.
fs plugin Rust scope management
In Rust, access the fs scope via app.fs_scope() and use methods like scope.allow_directory('/path/to/directory', false) to permit directory access, and scope.allowed() to check allowed paths.