#rocket #rocket-framework #multipart-form #web-server #web #form-data #server

rocket-multipart-form-data

此crate为Rocket框架提供了一个多部分解析器。

31次发布

0.10.7 2023年11月3日
0.10.5 2022年11月4日
0.10.3 2022年5月10日
0.10.2 2022年3月21日
0.3.1 2018年11月17日

#688 in 解析器实现

Download history 535/week @ 2024-03-13 771/week @ 2024-03-20 590/week @ 2024-03-27 690/week @ 2024-04-03 561/week @ 2024-04-10 782/week @ 2024-04-17 523/week @ 2024-04-24 524/week @ 2024-05-01 680/week @ 2024-05-08 542/week @ 2024-05-15 589/week @ 2024-05-22 551/week @ 2024-05-29 567/week @ 2024-06-05 669/week @ 2024-06-12 583/week @ 2024-06-19 474/week @ 2024-06-26

2,405 每月下载量
用于 2 crate

MIT 许可协议

37KB
605 代码行

Rocket框架的多部分表单数据

CI

此crate为Rocket框架提供了一个多部分解析器。

示例

#[macro_use] extern crate rocket;

use rocket::Data;
use rocket::http::ContentType;

use rocket_multipart_form_data::{mime, MultipartFormDataOptions, MultipartFormData, MultipartFormDataField, Repetition};

#[post("/", data = "<data>")]
async fn index(content_type: &ContentType, data: Data<'_>) -> &'static str {
    let mut options = MultipartFormDataOptions::with_multipart_form_data_fields(
        vec! [
            MultipartFormDataField::file("photo").content_type_by_string(Some(mime::IMAGE_STAR)).unwrap(),
            MultipartFormDataField::raw("fingerprint").size_limit(4096),
            MultipartFormDataField::text("name"),
            MultipartFormDataField::text("email").repetition(Repetition::fixed(3)),
            MultipartFormDataField::text("email"),
        ]
    );

    let mut multipart_form_data = MultipartFormData::parse(content_type, data, options).await.unwrap();

    let photo = multipart_form_data.files.get("photo"); // Use the get method to preserve file fields from moving out of the MultipartFormData instance in order to delete them automatically when the MultipartFormData instance is being dropped
    let fingerprint = multipart_form_data.raw.remove("fingerprint"); // Use the remove method to move raw fields out of the MultipartFormData instance (recommended)
    let name = multipart_form_data.texts.remove("name"); // Use the remove method to move text fields out of the MultipartFormData instance (recommended)
    let email = multipart_form_data.texts.remove("email");

    if let Some(file_fields) = photo {
        let file_field = &file_fields[0]; // Because we only put one "photo" field to the allowed_fields, the max length of this file_fields is 1.

        let _content_type = &file_field.content_type;
        let _file_name = &file_field.file_name;
        let _path = &file_field.path;

        // You can now deal with the uploaded file.
    }

    if let Some(mut raw_fields) = fingerprint {
        let raw_field = raw_fields.remove(0); // Because we only put one "fingerprint" field to the allowed_fields, the max length of this raw_fields is 1.

        let _content_type = raw_field.content_type;
        let _file_name = raw_field.file_name;
        let _raw = raw_field.raw;

        // You can now deal with the raw data.
    }

    if let Some(mut text_fields) = name {
        let text_field = text_fields.remove(0); // Because we only put one "text" field to the allowed_fields, the max length of this text_fields is 1.

        let _content_type = text_field.content_type;
        let _file_name = text_field.file_name;
        let _text = text_field.text;

        // You can now deal with the text data.
    }

    if let Some(text_fields) = email {
        for text_field in text_fields { // We put "email" field to the allowed_fields for two times and let the first time repeat for 3 times, so the max length of this text_fields is 4.
            let _content_type = text_field.content_type;
            let _file_name = text_field.file_name;
            let _text = text_field.text;

            // You can now deal with the text data.
        }
    }

    "ok"
}

另请参阅 examples.

Crates.io

https://crates.io/crates/rocket-multipart-form-data

文档

https://docs.rs/rocket-multipart-form-data

许可协议

MIT

依赖关系

~15–49MB
~798K SLoC