From 90b0e0db2914219820a6fe26cd83c50ba8342aec Mon Sep 17 00:00:00 2001 From: kouta Date: Tue, 30 May 2023 14:11:36 -0300 Subject: [PATCH] Doing only one call to field.data() can be incomplete, examples should iterate over it to explain how to read big files. --- examples/multipart.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/examples/multipart.rs b/examples/multipart.rs index 40548d1e2..43215b165 100644 --- a/examples/multipart.rs +++ b/examples/multipart.rs @@ -9,13 +9,18 @@ async fn main() { let route = warp::multipart::form().and_then(|form: FormData| async move { let field_names: Vec<_> = form .and_then(|mut field| async move { - let contents = - String::from_utf8_lossy(field.data().await.unwrap().unwrap().chunk()) - .to_string(); + let mut bytes: Vec = Vec::new(); + + // field.data() only returns a piece of the content, you should call over it until it replies None + while let Some(content) = field.data().await { + let content = content.unwrap(); + let chunk: &[u8] = content.chunk(); + bytes.extend_from_slice(chunk); + } Ok(( field.name().to_string(), field.filename().unwrap().to_string(), - contents, + String::from_utf8_lossy(&*bytes).to_string(), )) }) .try_collect()