reqwest icon indicating copy to clipboard operation
reqwest copied to clipboard

Adding file function to async::multipart::Form

Open visceralfield opened this issue 4 years ago • 5 comments

In the interest of feature parity between multipart::Form and async::multipart::Form, would a file function similar to the existing one for multipart::Form for async::multipart::Form be in scope for this library? I'm thinking something using tokio::fs::File. I have yet to explore implementing this but if there is interest I can start experimenting and open a PR.

visceralfield avatar Sep 20 '19 14:09 visceralfield

Yes, I think this is especially easier in current master using tokio 0.2, since then tokio::fs works in any executor.

I also at times wonder if it'd be feasible to combine the two multipart builders...

seanmonstar avatar Sep 20 '19 19:09 seanmonstar

Good to hear! I haven't used tokio directly much before, but I'll start a branch this weekend time permitting.

I think combining the multipart builders into one is worthwhile. I haven't looked at the internals of the builders, but logically separating Forms/Parts and the Client that sends them completely seems like a good design move.

visceralfield avatar Sep 20 '19 20:09 visceralfield

Is there any update for this? Thanks.

gliderkite avatar Jan 22 '20 15:01 gliderkite

You can do this with a few bits and pieces as I have done in lorikeet:

use tokio::fs::File;
use tokio_util::codec::{BytesCodec, FramedRead};

let file_name = path
    .file_name()
    .map(|val| val.to_string_lossy().to_string())
    .unwrap_or_default();

let file = File::open(&path)
    .await?;
    
let reader = Body::wrap_stream(FramedRead::new(file, BytesCodec::new()));
form.part(field_name, Part::stream(reader).file_name(file_name))

It would be nice to have this method back inside reqwest though.

cetra3 avatar Apr 21 '20 06:04 cetra3

You can do this with a few bits and pieces as I have done in lorikeet:

use tokio::fs::File;
use tokio_util::codec::{BytesCodec, FramedRead};

let file_name = path
    .file_name()
    .map(|val| val.to_string_lossy().to_string())
    .unwrap_or_default();

let file = File::open(&path)
    .await?;
    
let reader = Body::wrap_stream(FramedRead::new(file, BytesCodec::new()));
form.part(field_name, Part::stream(reader).file_name(file_name))

It would be nice to have this method back inside reqwest though.

thx for the codes, it save my life. especially for the .file_name(file_name)

taozhi8833998 avatar Mar 01 '22 08:03 taozhi8833998