36个发布版
0.7.0-beta.8 | 2024年7月9日 |
---|---|
0.7.0-beta.7 | 2024年3月28日 |
0.7.0-beta.6 | 2023年12月22日 |
0.7.0-beta.4 | 2023年7月18日 |
0.3.0 | 2018年7月25日 |
#58 在 HTTP服务器
703 每月下载量
用于 3 个Crate(2个直接)
345KB
894 行
Actix Form Data
一个用于从Actix Web的多部分流中检索表单数据的库。它可以流式传输上传的文件到文件系统(其主要目的),但它也可以解析相关表单数据。
用法
将其添加到您的依赖项中。
# Cargo.toml
[dependencies]
actix-web = "4.0.0"
actix-form-data = "0.6.0"
在项目中需要它。
// src/lib.rs or src/main.rs
use form_data::{Field, Form, Value};
概述
首先,您会创建一个您想从多部分流中解析的表单结构。
let form = Form::<()>::new().field("field-name", Field::text());
这创建了一个名为"field-name"的必填字段,它将被解析为文本。
然后,将其作为中间件传递。
App::new()
.wrap(form.clone())
.service(resource("/upload").route(post().to(upload)))
在您的处理程序中获取值
async fn upload(uploaded_content: Value<()>) -> HttpResponse {
...
}
并与之交互
let field_value = match value {
Value::Map(mut hashmap) => {
hashmap.remove("field-name")?;
}
_ => return None,
};
...
示例
/// examples/simple.rs
use actix_form_data::{Error, Field, Form, Value};
use actix_web::{
web::{post, resource},
App, HttpResponse, HttpServer,
};
use futures_util::stream::StreamExt;
async fn upload(uploaded_content: Value<()>) -> HttpResponse {
println!("Uploaded Content: {:#?}", uploaded_content);
HttpResponse::Created().finish()
}
#[actix_rt::main]
async fn main() -> Result<(), anyhow::Error> {
let form = Form::new()
.field("Hey", Field::text())
.field(
"Hi",
Field::map()
.field("One", Field::int())
.field("Two", Field::float())
.finalize(),
)
.field(
"files",
Field::array(Field::file(|_, _, mut stream| async move {
while let Some(res) = stream.next().await {
res?;
}
Ok(()) as Result<(), Error>
})),
);
println!("{:?}", form);
HttpServer::new(move || {
App::new()
.wrap(form.clone())
.service(resource("/upload").route(post().to(upload)))
})
.bind("127.0.0.1:8080")?
.run()
.await?;
Ok(())
}
贡献
您可以自由地提出您发现的问题。请注意,任何贡献的代码都将根据GPLv3许可。
许可证
版权 © 2021 Riley Trautman
Actix Form Data是免费软件:您可以在自由软件基金会发布的GNU通用公共许可证的条款下重新分发它和/或修改它,许可证版本为3,或者(根据您的选择)任何后续版本。
Actix Form Data是根据GNU通用公共许可证分发的,希望它会有所帮助,但没有任何保证;甚至没有关于适销性或适用于特定目的的暗示保证。有关更多详细信息,请参阅GNU通用公共许可证。此文件是Actix Form Data的一部分。
您应已收到GNU通用公共许可证的副本。如果没有,请参阅https://gnu.ac.cn/licenses/。
依赖项
~15–25MB
~453K SLoC