Uploading files to a Rocket server

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?

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());




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.

I tried your code and it works fine for me. Did you raise the limits for both data-form and file?

Thanks for the reply. I have now bumped form, data-form, and file up to 64MB each, but I'm still not seeing the file being written to the directory, only the 1192 bytes of metadata.

>> limits: bytes = 8KiB, data-form = 64MiB, file = 64MiB, form = 64MiB, json = 1MiB, msgpack = 1MiB, string = 8KiB

Can I ask how you tested it on your side? curl or a js page?

edit: I tried curl and it worked as well

Thanks, I have now got it working by changing the convoluted client-side 'blob' code (which worked with a Deno/TS project I had) to simply append the file to the form.

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.