I am trying to upload files to my Rocket v0.5-rc2 server and save them to a target directory with a unique name, but when I go to persist the TempFile I don't get the actual file, but around 1192 bytes of data which I think is a dump of the Form metadata instead (the actual PNG file is around 2.5Mbytes).
I have implemented using the examples from the Rocket website. I've also seen the Data
struct in Rocket and then setting a limit on the amount of bytes it will accept on a stream, but perhaps I have a misunderstanding of how the uploaded data is (or should be) being handled. Can I mix a Data
in with a derived FromForm
?
#[derive(FromForm)]
struct Upload<'f> {
file: TempFile<'f>
}
#[post("/upload", format = "multipart/form-data", data = "<form>")]
async fn upload_file(mut form: Form<Upload<'_>>) -> std::io::Result<()> {
println!("form.file = {:?}", form.file);
let file_id: String = Uuid::new_v4().hyphenated().encode_lower(&mut Uuid::encode_buffer()).to_owned();
let file_name = String::from(UPLOAD_DIR) + &file_id;
println!("destination = {}", file_name);
println!("length = {} bytes", form.file.len());
form.file.persist_to(file_name).await?;
Ok(())
}
This is my output on the console:
POST /upload multipart/form-data:
>> Matched: (upload_file) POST /upload multipart/form-data
form.file = File { file_name: Some(FileName("demo.png")), content_type: Some(ContentType(MediaType { source: Custom("image/png"), top: (0, 5), sub: (
6, 9), params: Dynamic([]) })), path: Left("C:\\<redacted>\\tmp/.tmpk2rVvk"), len: 1192 }
destination = C:\uploads\bbdad93b-22d0-45bb-87c2-266eb4f77055
length = 1192 bytes
>> Outcome: Success
>> Response succeeded.
Thanks for any help.