Here is my take:
// multipart_request.dart
import 'dart:async';
import 'package:http/http.dart' as http;
class MultipartRequest extends http.MultipartRequest {
/// Creates a new [MultipartRequest].
MultipartRequest(
String method,
Uri url, {
this.onProgress,
}) : super(method, url);
final void Function(int bytes, int totalBytes) onProgress;
/// Freezes all mutable fields and returns a single-subscription [ByteStream]
/// that will emit the request body.
http.ByteStream finalize() {
final byteStream = super.finalize();
if (onProgress == null) return byteStream;
final total = this.contentLength;
int bytes = 0;
final t = StreamTransformer.fromHandlers(
handleData: (List<int> data, EventSink<List<int>> sink) {
bytes += data.length;
onProgress(bytes, total);
sink.add(data);
},
);
final stream = byteStream.transform(t);
return http.ByteStream(stream);
}
}
Usage:
import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart' show MediaType;
import 'multipart_request.dart';
final uri = 'https://...';
final request = MultipartRequest(
'POST',
uri,
onProgress: (int bytes, int total) {
final progress = bytes / total;
print('progress: $progress ($bytes/$total)');
},
);
request.headers['HeaderKey'] = 'header_value';
request.fields['form_key'] = 'form_value';
request.files.add(
await http.MultipartFile.fromPath(
'field_name',
'path/to/file',
contentType: MediaType('image', 'jpeg'),
),
);
final streamedResponse = await request.send();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…