use std::io::Write; use actix_multipart::Multipart; use actix_web::{middleware, web, App, Error, HttpResponse, HttpServer}; use futures::{StreamExt}; async fn save_file(mut payload: Multipart) -> Result { // iterate over multipart stream while let Some(item) = payload.next().await { let mut field = item?; let content_type = field.content_disposition().unwrap(); let filename = content_type.get_filename().unwrap(); let filepath = format!("./tmp/{}", filename); let mut f = std::fs::File::create(filepath).unwrap(); // Field in turn is stream of *Bytes* object while let Some(chunk) = field.next().await { let data = chunk.unwrap(); let mut pos = 0; while pos < data.len() { let bytes_written = f.write(&data[pos..])?; pos += bytes_written; } } } Ok(HttpResponse::Ok().into()) } fn index() -> HttpResponse { let html = r#" Upload Test
"#; HttpResponse::Ok().body(html) } fn main() -> std::io::Result<()> { std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info"); std::fs::create_dir_all("./tmp").unwrap(); let ip = "0.0.0.0:3000"; HttpServer::new(|| { App::new() .wrap(middleware::Logger::default()) .service( web::resource("/") .route(web::get().to(index)) .route(web::post().to(save_file)), ) }) .bind(ip)? .run() }