36个发布版

0.7.0-beta.82024年7月9日
0.7.0-beta.72024年3月28日
0.7.0-beta.62023年12月22日
0.7.0-beta.42023年7月18日
0.3.0 2018年7月25日

#58HTTP服务器

Download history 289/week @ 2024-04-27 115/week @ 2024-05-04 283/week @ 2024-05-11 275/week @ 2024-05-18 244/week @ 2024-05-25 294/week @ 2024-06-01 262/week @ 2024-06-08 236/week @ 2024-06-15 279/week @ 2024-06-22 174/week @ 2024-06-29 248/week @ 2024-07-06 197/week @ 2024-07-13 169/week @ 2024-07-20 209/week @ 2024-07-27 158/week @ 2024-08-03 148/week @ 2024-08-10

703 每月下载量
用于 3 个Crate(2个直接)

GPL-3.0 许可证

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