上傳
透過 HTTP 從磁碟上傳檔案到遠端伺服器。從遠端 HTTP 伺服器下載檔案到磁碟。
支援平台
此插件需要至少 1.77.2 的 Rust 版本
平台 | 層級 | 註解 |
---|---|---|
windows | ||
linux | ||
macos | ||
android | ||
ios |
設定
使用您專案的套件管理器新增依賴項
npm run tauri add upload
yarn run tauri add upload
pnpm tauri add upload
deno task tauri add upload
bun tauri add upload
cargo tauri add upload
-
在
src-tauri
資料夾中執行以下指令,將插件新增到專案的Cargo.toml
中的依賴項cargo add tauri-plugin-upload -
修改
lib.rs
以初始化插件src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_upload::init()).run(tauri::generate_context!()).expect("error while running tauri application");} -
使用您偏好的 JavaScript 套件管理器安裝 JavaScript Guest 綁定
npm install @tauri-apps/plugin-uploadyarn add @tauri-apps/plugin-uploadpnpm add @tauri-apps/plugin-uploaddeno add npm:@tauri-apps/plugin-uploadbun add @tauri-apps/plugin-upload
使用方式
一旦您完成插件的註冊和設定流程,您就可以透過 JavaScript Guest 綁定存取其所有 API。
以下是如何使用插件上傳和下載檔案的範例
import { upload } from '@tauri-apps/plugin-upload';// when using `"withGlobalTauri": true`, you may use// const { upload } = window.__TAURI__.upload;
upload( 'https://example.com/file-upload', './path/to/my/file.txt', ({ progress, total }) => console.log(`Uploaded ${progress} of ${total} bytes`), // a callback that will be called with the upload progress { 'Content-Type': 'text/plain' } // optional headers to send with the request);
import { download } from '@tauri-apps/plugin-upload';// when using `"withGlobalTauri": true`, you may use// const { download } = window.__TAURI__.upload;
download( 'https://example.com/file-download-link', './path/to/save/my/file.txt', ({ progress, total }) => console.log(`Downloaded ${progress} of ${total} bytes`), // a callback that will be called with the download progress { 'Content-Type': 'text/plain' } // optional headers to send with the request);
權限
預設情況下,所有潛在危險的插件指令和範圍都被封鎖,無法存取。您必須修改 capabilities
組態中的權限才能啟用這些。
有關更多資訊,請參閱功能概述,以及使用插件權限的逐步指南。
{ "permissions": [ ..., "upload:default", ]}
預設權限
此權限集設定了可從上傳插件取得的操作類型。
已授權的權限
預設情況下,所有操作都已啟用。
allow-upload
allow-download
權限表
識別符 | 描述 |
---|---|
|
啟用下載指令,無需任何預先設定的範圍。 |
|
拒絕下載指令,無需任何預先設定的範圍。 |
|
啟用上傳指令,無需任何預先設定的範圍。 |
|
拒絕上傳指令,無需任何預先設定的範圍。 |
© 2025 Tauri 貢獻者。CC-BY / MIT