async fn file_upload_handler(req: Request<Body>) -> Result<Response<Body>, Error> {
    // Create some constraints to be applied to the fields to prevent DDoS attack.
    let constraints = Constraints::new()
        // We only accept `my_text_field` and `my_file_field` fields,
        // For any unknown field, we will throw an error.
        .allowed_fields(vec!["my_text_field", "my_file_field"])
        .size_limit(
            SizeLimit::new()
                // Set 15mb as size limit for the whole stream body.
                .whole_stream(15 * 1024 * 1024)
                // Set 10mb as size limit for all fields.
                .per_field(10 * 1024 * 1024)
                // Set 30kb as size limit for our text field only.
                .for_field("my_text_field", 30 * 1024),
        );

    // Convert the request into a `Multipart` instance.
    let mut multipart = match req.into_multipart_with_constraints(constraints) {
        Ok(m) => m,
        Err(err) => {
            return Ok(Response::builder()
                .status(StatusCode::BAD_REQUEST)
                .body(Body::from(format!("Bad Request: {}", err)))
                .unwrap());
        }
    };

    // Iterate over the fields.
    while let Some(mut field) = multipart.next_field().await.map_err(|err| Error::wrap(err))? {
        // Get the field name.
        let name = field.name();
        // Get the field's filename if provided in "Content-Disposition" header.
        let file_name = field.file_name();

        println!("Name {:?}, File name: {:?}", name, file_name);

        // Process the field data chunks e.g. store them in a file.
        while let Some(chunk) = field.chunk().await.map_err(|err| Error::wrap(err))? {
            // Do something with field chunk.
            println!("Chunk: {:?}", chunk);
        }
    }

    Ok(Response::new(Body::from("Success")))
}
// A handler to handle file uploading in `multipart/form-data` content-type.
A handler to handle file uploading in `multipart/form-data` content-type.
A handler to handle file uploading in ` multipart / form - data ` content - type .
async fn file_upload_handler(req: Request<Body>) -> Result<Response<Body>, Error> {
    // Create some constraints to be applied to the fields to prevent DDoS attack.
    let constraints = Constraints::new()
        // We only accept `my_text_field` and `my_file_field` fields,
        // For any unknown field, we will throw an error.
        .allowed_fields(vec!["my_text_field", "my_file_field"])
        .size_limit(
            SizeLimit::new()
                // Set 15mb as size limit for the whole stream body.
                .whole_stream(15 * 1024 * 1024)
                // Set 10mb as size limit for all fields.
                .per_field(10 * 1024 * 1024)
                // Set 30kb as size limit for our text field only.
                .for_field("my_text_field", 30 * 1024),
        );
    // Convert the request into a `Multipart` instance.
    let mut multipart = match req.into_multipart_with_constraints(constraints) {
        Ok(m) => m,
        Err(err) => {
            return Ok(Response::builder()
                .status(StatusCode::BAD_REQUEST)
                .body(Body::from(format!("Bad Request: {}", err)))
                .unwrap());
        }
    };
    // Iterate over the fields.
    while let Some(mut field) = multipart.next_field().await.map_err(|err| Error::wrap(err))? {
        // Get the field name.
        let name = field.name();
        // Get the field's filename if provided in "Content-Disposition" header.
        let file_name = field.file_name();
        println!("Name {:?}, File name: {:?}", name, file_name);
        // Process the field data chunks e.g. store them in a file.
        while let Some(chunk) = field.chunk().await.map_err(|err| Error::wrap(err))? {
            // Do something with field chunk.
            println!("Chunk: {:?}", chunk);
        }
    }
    Ok(Response::new(Body::from("Success")))
}
async fn file_upload_handler ( req : Request < Body > ) -> Result < Response < Body > , Error > { // Create some constraints to be applied to the fields to prevent DDoS attack. let constraints = Constraints :: new ( ) // We only accept `my_text_field` and `my_file_field` fields, // For any unknown field, we will throw an error. . allowed_fields ( vec ! [ " " " " ] ) . size_limit ( SizeLimit :: new ( ) // Set 15mb as size limit for the whole stream body. . whole_stream ( 15 * 1024 * 1024 ) // Set 10mb as size limit for all fields. . per_field ( 10 * 1024 * 1024 ) // Set 30kb as size limit for our text field only. . for_field ( " " , 30 * 1024 ) , ) ; // Convert the request into a `Multipart` instance. let mut multipart = match req . into_multipart_with_constraints ( constraints ) { Ok ( m ) => m , Err ( err ) => { return Ok ( Response :: builder ( ) . status ( StatusCode :: BAD_REQUEST ) . body ( Body :: from ( format ! ( " " err ) ) ) . unwrap ( ) ) ; } } ; // Iterate over the fields. while let Some ( mut field ) = multipart . next_field ( ) . await . map_err ( | err | Error :: wrap ( err ) ) ? { // Get the field name. let name = field . name ( ) ; // Get the field's filename if provided in "Content-Disposition" header. let file_name = field . file_name ( ) ; println ! ( " " name file_name ) ; // Process the field data chunks e.g. store them in a file. while let Some ( chunk ) = field . chunk ( ) . await . map_err ( | err | Error :: wrap ( err ) ) ? { // Do something with field chunk. println ! ( " " chunk ) ; } } Ok ( Response :: new ( Body :: from ( " " ) ) ) }
A handler to handle file uploading in `multipart/form-data` content-type.
A handler to handle file uploading in ` multipart / form - data ` content - type .
req
Request<Body>