Skip to content

Commit

Permalink
Doing only one call to field.data() can be incomplete, examples shoul…
Browse files Browse the repository at this point in the history
…d iterate over it to explain how to read big files. (#1044)
  • Loading branch information
kouta-kun committed Jun 1, 2023
1 parent 4c1f7ba commit f939404
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions examples/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> = 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()
Expand Down

0 comments on commit f939404

Please sign in to comment.