上传
通过 HTTP 将文件从磁盘上传到远程服务器。从远程 HTTP 服务器下载文件到磁盘。
🌐 Upload files from disk to a remote server over HTTP. Download files from a remote HTTP server to disk.
🌐 Supported Platforms
This plugin requires a Rust version of at least 1.77.2
| Platform | Level | Notes |
|---|---|---|
| windows | ||
| linux | ||
| macos | ||
| android | ||
| ios |
🌐 Setup
使用项目的包管理器添加依赖:
npm run tauri add uploadyarn run tauri add uploadpnpm tauri add uploaddeno task tauri add uploadbun tauri add uploadcargo 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
🌐 Usage
完成插件的注册和设置过程后,你可以通过 JavaScript 来宾绑定访问其所有 API。
🌐 Once you’ve completed the registration and setup process for the plugin, you can access all of its APIs through the JavaScript guest bindings.
以下是如何使用插件上传和下载文件的示例:
🌐 Here’s an example of how you can use the plugin to upload and download files:
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);🌐 Permissions
默认情况下,所有潜在危险的插件命令和作用域都会被阻止,无法访问。你必须在 capabilities 配置中修改权限以启用这些功能。
🌐 By default all potentially dangerous plugin commands and scopes are blocked and cannot be accessed. You must modify the permissions in your capabilities configuration to enable these.
有关更多信息,请参见功能概览,有关使用插件权限的分步指南,请参见分步指南。
🌐 See the Capabilities Overview for more information and the step by step guide to use plugin permissions.
{ "permissions": [ ..., "upload:default", ]}Default Permission
This permission set configures what kind of operations are available from the upload plugin.
Granted Permissions
All operations are enabled by default.
This default permission set includes the following:
allow-uploadallow-download
Permission Table
| Identifier | Description |
|---|---|
|
|
Enables the download command without any pre-configured scope. |
|
|
Denies the download command without any pre-configured scope. |
|
|
Enables the upload command without any pre-configured scope. |
|
|
Denies the upload command without any pre-configured scope. |
Tauri 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站