Skip to main content

FromMultipart

Derive Macro FromMultipart 

Source
#[derive(FromMultipart)]
{
    // Attributes available to this derive:
    #[upload]
    #[serde]
}
Expand description

Derive macro for parsing multipart/form-data into a struct.

Can only be derived for structs with named fields. Generates an implementation of modo_upload::FromMultipart, which is used by the MultipartForm extractor.

§Supported field types

Rust typeBehaviour
UploadedFileRequired file field; errors if absent
Option<UploadedFile>Optional file field
Vec<UploadedFile>Zero or more files under the same field name
BufferedUploadRequired buffered upload (only one per struct)
StringRequired text field
Option<String>Optional text field
any T: FromStrRequired text field, parsed via FromStr

§Field attributes

§#[upload(...)]

Controls validation applied to file fields. All sub-attributes are optional.

  • max_size = "<size>" — maximum file size, e.g. "5mb", "100kb", "2gb". Size strings are case-insensitive and accept the suffixes b, kb, mb, gb. A plain integer is treated as bytes.
  • accept = "<pattern>" — MIME type pattern, e.g. "image/*", "application/pdf".
  • min_count = <n> — minimum number of files for Vec<UploadedFile> fields.
  • max_count = <n> — maximum number of files for Vec<UploadedFile> fields.

§#[serde(rename = "...")]

Overrides the multipart field name used for matching. By default the Rust field name is used as the multipart field name.

§Example

use modo_upload::{FromMultipart, UploadedFile};

#[derive(FromMultipart)]
struct ProfileForm {
    #[upload(max_size = "5mb", accept = "image/*")]
    avatar: UploadedFile,

    name: String,

    #[upload(min_count = 1, max_count = 5)]
    attachments: Vec<UploadedFile>,

    #[serde(rename = "user_email")]
    email: Option<String>,
}