Use the multipart crate with a web framework like actix-web to handle file uploads. Add actix-web and multipart to your Cargo.toml, then define a handler that extracts the file from the request body and saves it to disk.
use actix_web::{web, App, HttpServer, HttpResponse, Responder};
use actix_multipart::Multipart;
use futures_util::StreamExt;
use std::io::Write;
async fn upload(mut payload: Multipart) -> impl Responder {
while let Some(item) = payload.next().await {
let mut file = item.unwrap();
let filename = file.content_disposition().unwrap().get_filename().unwrap();
let mut f = std::fs::File::create(filename).unwrap();
while let Some(chunk) = file.next().await {
f.write_all(&chunk.unwrap()).unwrap();
}
}
HttpResponse::Ok().body("File uploaded")
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new().route("/upload", web::post().to(upload))
})
.bind("127.0.0.1:8080")?
.run()
.await
}