Hi,
I would like to display upload progress for large files but I don't see any way to do that.
You mentionned something in #222 for download but it seems like it cannot be used for upload. Is there any plan to adding that or a secret way :smile: ?
Hm, if you're streaming the upload by prodiving a Read, you could do this. I'd make a custom type that wraps your body, and implements Read. In the read function, you can read from the wrapped body, and then signal however you like that N bytes out of the total were just "sent".
@seanmonstar I tried but I really can't get my head around that. Could you make a simple example ?
struct UploadProgress<R> {
inner: R,
bytes_read: usize,
total: usize,
}
impl<R: Read> Read for UploadProgress<R> {
fn read(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.read(buf)
.map(|n| {
self.bytes_read += n;
println!("Upload progress: {}/{} bytes ({}%)", self.bytes_read, self.total, self.bytes_read * 100 / self.total);
n
})
}
}
let body = UploadProgress {
bytes_read: 0,
total: some_number,
inner: some_reader, // like a `File`
};
client.post(uri)
.body(Body::sized(body, some_numer))
.send()?;
Thank you, gonna try it soon :+1:
Most helpful comment