上传
通过 HTTP 从磁盘上传文件到远程服务器。从远程 HTTP 服务器下载文件到磁盘。
设置
这个插件要求 Rust 版本至少是 1.75
使用项目的包管理器来添加依赖。
npm run tauri add upload
yarn run tauri add upload
pnpm tauri add upload
bun tauri add upload
cargo tauri add upload
-
运行
cargo add tauri-plugin-upload
命令,将插件添加到项目的Cargo.toml
依赖中。 -
修改
lib.rs
来初始化插件。src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pb fn run() {tauri::Builder::default()// Initialize the plugin.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-uploadbun add @tauri-apps/plugin-upload
用法
一旦完成了插件的注册和设置过程,就可以通过 JavaScript Guest 绑定访问它的所有 API。
下面是一个使用插件上传和下载文件的例子:
import { upload } from '@tauri-apps/plugin-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' } // 与请求一起发送的可选头信息);
import { download } from '@tauri-apps/plugin-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' } // 与请求一起发送的可选头信息);
© 2024 Tauri Contributors. CC-BY / MIT