Flutter如何使用Google Drive API获取上传文件到Google Drive的进度状态?

ezykj2lf  于 2023-11-21  发布在  Flutter
关注(0)|答案(2)|浏览(156)

我正在使用google_apis v3上传文件到驱动器。我如何通过Google Drive API库获取上传状态的进度百分比?以及如何取消上传文件?
示例源代码如下。

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;
  }

字符串

omjgkv6w

omjgkv6w1#

试试这个

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

字符串

d5vmydt9

d5vmydt92#

@Werner Scholtz的回答应该被接受。我只是想在这里记录如何在创建Google Drive Media类型的文件时集成这种方法:

var media = drive.Media(
    file.openRead().transform(
          StreamTransformer.fromHandlers(
            handleData: (data, sink) {
              log('Something');
              bytesUploaded += data.length;
              sink.add(data);
            },
            handleError: (error, stackTrace, sink) {},
            handleDone: (sink) {
              log('Something done');
              sink.close();
            },
          ),
        ),
    totalBytes);

字符串
我上传了它:

var response = await driveApi.files.create(driveFile, uploadMedia: media);

相关问题