ActixExtractMultipart

Functions and structures to handle actix multipart more easily. You can convert the multipart into a struct.

To use this function, you need to create a structure with "Deserialize" trait, like this: ```rust

[derive(Deserialize)]

struct Example { stringparam: String, optionaluparam: Option, filesparam: Option> } File is a structure for any files: rust

[derive(Debug, Deserialize)]

pub struct File { filetype: String, name: String, data: FileData, } impl File { pub fn filetype(&self) -> &String { &self.file_type } pub fn name(&self) -> &String { &self.name } pub fn len(&self) -> usize { self.data.len() } pub fn data(&self) -> &FileData { &self.data } } FileData is an alias to Vec<u8> bytes: rust pub type FileData = Vec; ```

Example of use

```rust use actixweb::{post, App, HttpResponse, HttpServer}; use serde::{Deserialize}; use actixextract_multipart::*;

[derive(Deserialize)]

struct Example { stringparam: String, optionaluparam: Option, fileparam: File }

fn savingfilefunction(file: &File) -> Result<(), ()> { // Do some stuff here println!("Saving file \"{}\" successfully", file.name());

Ok(())

}

[post("/example")]

async fn index(examplestructure: Multipart::) -> HttpResponse {
println!("Value of string
param: {}", examplestructure.stringparam); println!("Value of optionaluparam: {:?}", examplestructure.optionaluparam); println!("Having file? {}", match examplestructure.fileparam { Some() => "Yes", None => "No" });

if let Some(file) = &example_structure.file_param {
    match saving_file_function(&file) {
        Ok(_) => println!("File saved!"),
        Err(_) => println!("An error occured while file saving")
    }
}

HttpResponse::Ok().json("Done")

}

[actix_web::main]

async fn main() -> std::io::Result<()> { println!("Server run at http://127.0.0.1:8080");

HttpServer::new(move || {
    App::new()
        .service(index)
})
.bind(("127.0.0.1", 8080))?
.run()
.await

} In this example, if you dont have received a file, extract_multipart will return an Err(_), because data don't correspond to the data struct "Example". If the File is optional, you can simply set the type as Option<File>, like this: rust

[derive(Deserialize)]

struct Example { stringparam: String, optionaluparam: Option, fileparam: Option } In the case of Vec<File>, don't forget to put hooks at the end of the field name. You can also have any other type array like Vec<String>, Vec<i32> etc... In the follow html exemple, you can notice that the file's field's name contain hooks: name="files_param[]". It's important, without hooks, this code will not work. html Testing

```