Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Field::chunk #901

Merged
merged 8 commits into from
Apr 4, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions axum/src/extract/multipart.rs
Expand Up @@ -142,6 +142,38 @@ impl<'a> Field<'a> {
pub async fn text(self) -> Result<String, MultipartError> {
self.inner.text().await.map_err(MultipartError::from_multer)
}

/// Stream a chunk of the field data.
///
/// When the field data has been exhausted, this will return [`None`].
///
/// This does the exact same thing as the `Stream` impl and is for convenience
davidpdrsn marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Examples
davidpdrsn marked this conversation as resolved.
Show resolved Hide resolved
///
/// ```
/// use axum::{
/// extract::Multipart,
/// routing::post,
/// Router,
/// };
///
/// async fn upload(mut multipart: Multipart) {
/// while let Some(mut field) = multipart.next_field().await.unwrap() {
/// while let Some(chunk) = field.chunk().await.unwrap() {
davidpdrsn marked this conversation as resolved.
Show resolved Hide resolved
davidpdrsn marked this conversation as resolved.
Show resolved Hide resolved
/// println!("Chunk: {:?}", chunk);
davidpdrsn marked this conversation as resolved.
Show resolved Hide resolved
/// }
/// }
/// }
///
/// let app = Router::new().route("/upload", post(upload));
/// ```
pub async fn chunk(&mut self) -> Result<Option<Bytes>, MultipartError> {
self.inner
.chunk()
.await
.map_err(MultipartError::from_multer)
}
}

/// Errors associated with parsing `multipart/form-data` requests.
Expand Down