29 Upload

原文链接: https://tauri.app/plugin/upload/

通过 HTTP 上传与下载文件。

支持的平台

平台支持程度说明
Windows完整支持
Linux完整支持
macOS完整支持
Android完整支持
iOS完整支持

设置

自动

使用你的项目包管理器添加依赖:

1
npm run tauri add upload
1
yarn run tauri add upload
1
pnpm tauri add upload
1
deno task tauri add upload
1
bun tauri add upload
1
cargo tauri add upload

手动

  1. 在 src-tauri 文件夹中运行以下命令,把插件加入 Cargo.toml 里的项目依赖:

    1
    
    cargo add tauri-plugin-upload
    
  2. 修改 lib.rs 初始化插件:

    1
    2
    3
    4
    5
    6
    7
    
    #[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");
    }
    
  3. 用你偏好的 JavaScript 包管理器安装 JavaScript 端绑定:

1
npm install @tauri-apps/plugin-upload
1
yarn add @tauri-apps/plugin-upload
1
pnpm add @tauri-apps/plugin-upload
1
deno add npm:@tauri-apps/plugin-upload
1
bun add @tauri-apps/plugin-upload

用法

完成插件的注册与设置之后,你可以通过 JavaScript 端绑定访问它的所有 API。

下面是一个使用该插件上传和下载文件的示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import { upload } from '@tauri-apps/plugin-upload';
// 使用 `"withGlobalTauri": true` 时,你可以这样写
// 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`), // 上传进度回调
  { 'Content-Type': 'text/plain' } // 可选:随请求发送的头
);
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import { download } from '@tauri-apps/plugin-upload';
// 使用 `"withGlobalTauri": true` 时,你可以这样写
// 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`), // 下载进度回调
  { 'Content-Type': 'text/plain' } // 可选:随请求发送的头
);

权限

默认情况下,所有有潜在危险的插件命令和作用域都被阻止,无法访问。你必须在 capabilities 配置中修改权限才能启用它们。

更多信息请参阅能力概述,以及使用插件权限的分步指南。

1
2
3
4
5
6
{
  "permissions": [
    ...,
    "upload:default"
  ]
}
最后修改 September 26, 2026: 更新 (630b11f59)