3

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;
  }
James Z
  • 12,209
  • 10
  • 24
  • 44
hayashi
  • 81
  • 5

1 Answers1

1

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();
      },
   ),
);