async-graphql / examples

217 stars 54 forks source link

Upload file to folder #27

Closed benjamingb closed 3 years ago

benjamingb commented 3 years ago

How can I move the file to a folder?

I am using the following example

    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 Actix-web 3.1

sunli829 commented 3 years ago

You can use the UploadValue::into_read function to get std::io::Read, and then write the content to a new file.

benjamingb commented 3 years ago

with UploadValue::into_read I can read but only .csv files,

  let mut content = String::new();
            upload.into_read().read_to_string(&mut content).unwrap(); 

I would like to upload images and put it on a disk path. I am new to Rust and I have not managed to do it, can you guide me?

sunli829 commented 3 years ago

with UploadValue::into_read I can read but only .csv files,

  let mut content = String::new();
            upload.into_read().read_to_string(&mut content).unwrap(); 

I would like to upload images and put it on a disk path. I am new to Rust and I have not managed to do it, can you guide me?

You should use Read::read_to_end to read binary files.