Actix-web has an example on their github page with this code. https://github.com/actix/examples/blob/master/forms/multipart/src/main.rs
The code
#[derive(Debug, MultipartForm)]
struct UploadForm {
#[multipart(rename = "file")]
files: Vec<TempFile>,
}
async fn save_files(
MultipartForm(form): MultipartForm<UploadForm>,
) -> Result<impl Responder, Error> {
for f in form.files {
let path = format!("./tmp/{}", f.file_name.unwrap());
log::info!("saving to {path}");
f.file.persist(path).unwrap();
}
Ok(HttpResponse::Ok())
}
I understand and use this code as an upload function in my project, which saves files to a folder called uploads. This function accepts multipart/form-data and saves the "files" array to disk. However, I cannot figure out how to create the accompanying download function.
Or in my case a preview function. That takes all content from the uploads folder and send them back to the frontend in the form of multipart/form-data, to show a preview of files uploaded.
( extra context, I dont send the original file back, but an svg for now. However, the type of file should not matter, my issue is sending the multipart/form-data from backend to frontend )
I use actix-web ^4, and actix-multipart 0.6.0
I tried to both creating content disposition myself, using actix_multipart::Multipart::new, which takes in a headerMap and a stream (Which I could not figure out how to send through the HttpResponse) And every link I could find talking about actix multipart on google, but most of those only talk about upload, none of them about download. Even asked chat-gpt which was no help as always :)