Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

This crate provides a multipart parser for the Rocket framework.

License

NotificationsYou must be signed in to change notification settings

magiclen/rocket-multipart-form-data

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CI

This crate provides a multipart parser for the Rocket framework.

Example

#[macro_use]externcrate rocket;use rocket::Data;use rocket::http::ContentType;use rocket_multipart_form_data::{mime,MultipartFormDataOptions,MultipartFormData,MultipartFormDataField,Repetition};#[post("/", data ="<data>")]asyncfnindex(content_type:&ContentType,data:Data<'_>) ->&'staticstr{letmut 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"),]);letmut 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 droppedlet 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");ifletSome(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.}ifletSome(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.}ifletSome(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.}ifletSome(text_fields) = email{for text_fieldin 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"}

Also seeexamples.

Crates.io

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

Documentation

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

License

MIT

About

This crate provides a multipart parser for the Rocket framework.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp