0

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 :)

Photic
  • 11
  • 2
  • Not sure what you mean, why don't you just serve the file like any other file? See [How to efficiently serve a file with actix-web](https://stackoverflow.com/questions/70614240/how-to-efficiently-serve-a-file-with-actix-web) – cafce25 Mar 20 '23 at 18:08
  • So that post handles 1 file. So basically I want to do the same, with a Vec. However, #[get("/preview")] async fn get_pdf_preview(req: HttpRequest) -> Result> A vector of named files does not comply with the responder trait. "the trait bound `Vec: Responder` is not satisfied the trait `Responder` is implemented for `Vec` required for `Result, actix_web::Error>` to implement `Responder`" – Photic Mar 20 '23 at 18:27

1 Answers1

0

I am going to perform this task in another way. Going for single file requests using NamedFile. Just wanted to optimise for less requests. Might pick it up later and update. For now it will have to do.

Photic
  • 11
  • 2