Save the uploaded file to a folder

Hi
With this function I receive a file via graphql, How do I save the received content to a folder?

 async fn single_upload(&self, ctx: &Context<'_>, file: Upload) -> FileInfo {
        let mut storage = ctx.data_unchecked::<FileStorage>().lock().await;
        println!("files count: {}", storage.len());
        let entry = storage.vacant_entry();
        let upload = file.value(ctx).unwrap();
        let info = FileInfo {
            id: entry.key().into(),
            filename: upload.filename.clone(),
            mimetype: upload.content_type,
        };
       
        entry.insert(info.clone());
        info
    }

I am using async-graphql and actix-web

You have to create the file to write it to, for example:

use std::fs::File;

let mut saved_file = File::create(
    format!("uploaded_files/{}", upload.filename)
);

And then you can copy all of the contents over:

std::io::copy(&mut upload, &mut saved_file);

However note that that is a blocking operation, which you must not do in an async context. Runtimes generally provide a way to spawn a blocking task, such as Tokio's spawn_blocking, or blocking::unblock (which is used internally by Smol and async-std).

In the case of Tokio, you can just use tokio::fs::File and tokio::io::copy instead to make it asynchronous.