Created
December 26, 2018 12:53
-
-
Save dru/85975bf55151e7f160a10cdd3575e9be to your computer and use it in GitHub Desktop.
Upload progress with Dart and Streams
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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