Upload pdf with Multipartformdata

H,
I am trying to upload multiple files with multipartform data. But the data is always empty

I am trying to upload pdf with multipartformdata.
#[post("/upload-pdf", data = "")]
pub async fn upload_pdf(content_type:&ContentType,data: Data<'_>) -> &'static str {
println!("Data is {:?} ",content_type);
let mut options = MultipartFormDataOptions::with_multipart_form_data_fields(
vec! [
MultipartFormDataField::file("pdf_file").content_type_by_string(Some(mime::APPLICATION_PDF.to_string())).unwrap(),
MultipartFormDataField::file("doc_file").content_type_by_string(Some(mime::MULTIPART_FORM_DATA.to_string())).unwrap(),
]
);

let mut multipart_form_data = match MultipartFormData::parse(content_type, data, options).await {
    Ok(multipart)=>multipart,
    Err(_) => return "Failed to parse multipart/form-data.",
};

println!("multipart dta{:?}",multipart_form_data);

let pdf_file =  multipart_form_data.files.get("pdf_file");
//let doc_file =  multipart_form_data.files.get("doc_file");


println!("file {:?}",pdf_file);

if let Some(file_fields) = pdf_file {

    let file_field = &file_fields[0]; 
    let _content_type = &file_field.content_type;
    let _file_name = &file_field.file_name;
    let _path = &file_field.path;

    println!("File name {:?}",_file_name);

    // You can now deal with the uploaded file.
}

/*if let Some(file_fields) = doc_file {
    let file_field = &file_fields[0]; // Because we only put one "photo" field to the allowed_fields, the max length of this file_fields is 1.

    let _content_type = &file_field.content_type;
    let _file_name = &file_field.file_name;
    let _path = &file_field.path;

    // You can now deal with the uploaded file.
}*/

Here data is always empty

This is not nearly enough information for us to help. You'll have to provide concrete code (a minimal, compiling, self-contained example) and describe exactly what you are doing and what is happening instead.

2 Likes

Super short questions that are impossible to interpret seems to be a reoccurring theme in your topics. In case you are interested in receiving useful answers, you'll need to give a lot more information as to what you're talking about.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.