Skip to content

Simpler alternative to TempFile in multipart form file upload (Like using Vec<u8> directly) #2148

@Alvenix

Description

@Alvenix

For file upload, I have been using TempFile. However, I don't feel that it is very ergonomic. You have to store the file first to read it and later delete it.

Here is some pseudo code which showcase how sometimes I had to use it.

#[post("/upload", data = "<file>")]
async fn process_file(mut file: TempFile<'_>) -> std::io::Result<()> {
    let uuid = Uuid::new_v4();
    let random_file_name = format!("/tmp/{uuid}");
    file.persist_to(&random_file_name).await?;
  
    let content = fs::read(&random_file_name)?;

    fs::delete(&random_file_name)?;

    Ok(())
}

I did not find existing alternative in Rocket.

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedAn accepted request or suggestion

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions