I am using google_apis v3 to upload a file to drive. How can I get the progress percentage of uploading status through the Google Drive API library? And also how can I cancel the uploading file?
The sample source code is as below.
Future<File> uploadFile(
GoogleAuthClient googleAuthClient, FileUpload fileUpload) async {
final DriveApi driveApi = driveDataProvider.getDriveApi(googleAuthClient);
final Stream<List<int>> mediaStream =
Future.value(fileUpload.data).asStream().asBroadcastStream();
Media media = Media(mediaStream, fileUpload.size);
File createdFile = await driveApi.files.create(fileUpload.file,
uploadMedia: media, uploadOptions: UploadOptions.resumable;);
return createdFile;
}
Try something like this.
int byteCount = 0;
Stream<List<int>> mediaStream = file.openRead().transform(
StreamTransformer.fromHandlers(handleData: (data, sink) {
byteCount += data.length;
//add a callback function here.
// callback(byteCount);
sink.add(data);
}, handleError: (error, stackTrace, sink) {
//Handle error
}, handleDone: (sink) {
sink.close();
},
),
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With