multipart method
Parses the incoming streaming multipart/form-data request body.
Yields each BloomMultipartPart (BloomMultipartField or BloomMultipartFile)
as it is parsed from the stream without buffering entire files into memory.
Throws StateError if this request is not a streaming request or if the request body stream has already been consumed. Throws FormatException if the Content-Type header or multipart payload is malformed.
Example
await for (final part in request.multipart()) {
if (part is BloomMultipartField) {
print('${part.name}: ${part.value}');
} else if (part is BloomMultipartFile) {
await File('uploads/${part.filename}').openWrite().addStream(part.bytes);
}
}
Implementation
Stream<BloomMultipartPart> multipart({int? maxBytes}) {
if (_streamBody == null) {
throw StateError(
'BloomRequest.multipart() called on a non-streaming request. '
'Ensure the request was sent with multipart/form-data and not pre-buffered.',
);
}
if (_bodyStreamTaken) {
throw StateError(
'BloomRequest streaming body has already been consumed. '
'A request stream may only be read once.',
);
}
final contentType = headers['content-type'] ?? '';
final boundary = extractMultipartBoundary(contentType);
_bodyStreamTaken = true;
return parseMultipartStream(
stream: _streamBody,
boundary: boundary,
maxBytes: maxBytes ?? _maxRequestBodyBytes,
);
}