2 个版本
0.1.5 | 2022 年 8 月 18 日 |
---|---|
0.1.4 | 2022 年 7 月 4 日 |
#893 in HTTP 服务器
40 个月下载量
12KB
225 行
actix-multipart-extract
这个包是一个 Rust 库,为 actix 4 提供适当的multipart 支持。
它可以将 multipart 请求解析为结构体并验证请求属性。它使用 serde 进行反序列化和 MultipartForm
derive。
安装
将 actix_multipart_extract
添加到您的 Cargo.toml
[dependencies]
actix-multipart-extract = "0.5"
示例
use actix_multipart_extract::{File, Multipart, MultipartForm};
use actix_web::{post, App, HttpResponse, HttpServer, Responder};
use serde::Deserialize;
// File, String, bool, and number types are the only supported types for forms.
// Vec and Option may also be used with one of the 4 types as the type param.
// Some serde attributes will work with forms.
#[derive(Deserialize, MultipartForm, Debug)]
pub struct ExampleForm {
#[multipart(max_size = 5MB)]
file_field: File,
string_field: Vec<String>, // list field
bool_field: Option<bool>, // optional field
}
#[post("/example")]
async fn example(example_form: Multipart<ExampleForm>) -> impl Responder {
// File field
println!("File size: {}", example_form.file_field.bytes.len());
println!(
"File content type: {}",
example_form.file_field.content_type
);
println!("File name: {}", example_form.file_field.name);
// List of strings field
println!("List of strings: {:?}", example_form.string_field);
// Optional bool field
match example_form.bool_field {
Some(v) => println!("Has bool field: {}", v),
None => println!("No bool field"),
}
HttpResponse::Ok()
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(move || App::new().service(example))
.bind(("127.0.0.1", 8080))?
.run()
.await
}
依赖
~17–28MB
~502K SLoC