上传
通过HTTP将文件从磁盘上传到远程服务器。从远程HTTP服务器将文件下载到磁盘。
支持的平台
该插件至少需要Rust版本 1.77.2
平台 | 级别 | 备注 |
---|---|---|
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客户端绑定
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客户端绑定访问其所有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", ]}
默认权限
此权限集配置了上传插件可用的操作类型。
已授予的权限
默认情况下,所有操作均启用。
允许上传
允许下载
权限表
标识符 | 描述 |
---|---|
|
启用下载命令,无需预设作用域。 |
|
拒绝下载命令,无需预设作用域。 |
|
启用上传命令,无需预设作用域。 |
|
拒绝上传命令,无需预设作用域。 |
© 2025 Tauri贡献者。CC-BY / MIT