//! Run with //! //! ```not_rust //! cd examples && cargo run -p example-multipart-form //! ``` use axum::{ extract::{ContentLengthLimit, Multipart}, response::Html, routing::get, Router, }; use std::net::SocketAddr; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; #[tokio::main] async fn main() { tracing_subscriber::registry() .with(tracing_subscriber::EnvFilter::new( std::env::var("RUST_LOG") .unwrap_or_else(|_| "example_multipart_form=debug,tower_http=debug".into()), )) .with(tracing_subscriber::fmt::layer()) .init(); // build our application with some routes let app = Router::new() .route("/", get(show_form).post(accept_form)) .layer(tower_http::trace::TraceLayer::new_for_http()); // run it with hyper let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); tracing::debug!("listening on {}", addr); axum::Server::bind(&addr) .serve(app.into_make_service()) .await .unwrap(); } async fn show_form() -> Html<&'static str> { Html( r#"
"#, ) } async fn accept_form( ContentLengthLimit(mut multipart): ContentLengthLimit< Multipart, { 250 * 1024 * 1024 /* 250mb */ }, >, ) { while let Some(field) = multipart.next_field().await.unwrap() { let name = field.name().unwrap().to_string(); let file_name = field.file_name().unwrap().to_string(); let content_type = field.content_type().unwrap().to_string(); let data = field.bytes().await.unwrap(); println!( "Length of `{}` (`{}`: `{}`) is {} bytes", name, file_name, content_type, data.len() ); } }