Skip to content

Instantly share code, notes, and snippets.

@dru
Created December 26, 2018 12:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dru/85975bf55151e7f160a10cdd3575e9be to your computer and use it in GitHub Desktop.
Save dru/85975bf55151e7f160a10cdd3575e9be to your computer and use it in GitHub Desktop.
Upload progress with Dart and Streams
import "dart:io";
import "dart:async";
main() async {
HttpClientRequest request =
await HttpClient().post('some.host', 8000, '/upload');
request.headers.contentType = ContentType.binary;
final file = File('/some/big/file');
final stream = file.openRead();
final FileStat stat = await file.stat();
var uploaded = 0;
await request.addStream(stream.map((chunk) {
uploaded += chunk.length;
print("Uploaded ${((uploaded / stat.size) * 100.0).toStringAsFixed(2)}%");
return chunk;
}));
request.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment