google / googleapis.dart

Repository for building the googleapis packages
https://pub.dev/packages/googleapis
BSD 3-Clause "New" or "Revised" License
392 stars 118 forks source link

Google Drive V3: Media are corrupted when downloaded with 'get' #75

Closed MichelMelhem closed 5 years ago

MichelMelhem commented 5 years ago

Hello, When i download a media with the get(fileId) method i have a problem, They are all corrupted when i try to read the file i downloaded previously. I first get the media from google drive, And them i write the media with writeAsByte on the local system with this code :

 drive.Media data = await MainProvider().downloadPlan(event.id);
        plansDir.create();
        final file = new File('${plansDir.path}/${event.filename}');
        data.stream.listen((data) async { // <-- here i write the file and when i read it he is corrupted
          await file.writeAsBytes(
            data,
            mode: FileMode.write,
          );

I use an authenthicate OAuth 2.0 client. It is a big issue beacause i can't download any Media from google drive. When i list files on google drive it work perfectly so my client is well authenticate.

This is the function that download the Meida from google drive :

drive.Media data = await MainProvider.driveApi.files
        .get(id, downloadOptions: drive.DownloadOptions.FullMedia);

    return data;

Thank you, Michel M

I'm using: Flutter : 1.5.4-hotfix.2 googleapis: ^0.53.0

jonasfj commented 5 years ago

It's fully possible that there is a bug here, but you should start by writing the stream to disk correctly.

Afaik, you cannot away in the .listen callback.

try something like:

final writer = File('$filename').openWrite();
await data.stream(writer);
await writer.close();
MichelMelhem commented 5 years ago

When i do like this :

        final plansDir = new Directory(
            '${workSiteBloc.currentState.workSites[event.index].path}/plans');
        yield currentState.copyWith(isFetching: true);
        drive.Media data = await GApiRepo().downloadPlan(event.id);
        plansDir.create();
        final file = new File('${plansDir.path}/${event.filename}').openWrite();
        data.stream(file);
        file.close();

        yield currentState.copyWith(isFetching: false);
        workSiteBloc.addPlan(event.index, event.id, event.filename, file.path);

I get this error : image

MichelMelhem commented 5 years ago

I fix my issue doing things like this :

    drive.Media data = await GApiRepo().downloadPlan(event.id);
        plansDir.create();
        final file = new File('${plansDir.path}/${event.filename}');
        final writer = File('${file.path}').openWrite();
        await writer.addStream(data.stream);
        await writer.close();

Thnak you for your help ^^

wengxianxun commented 5 months ago

我通过这样做来解决我的问题:

    drive.Media data = await GApiRepo().downloadPlan(event.id);
        plansDir.create();
        final file = new File('${plansDir.path}/${event.filename}');
        final writer = File('${file.path}').openWrite();
        await writer.addStream(data.stream);
        await writer.close();

谢谢你的帮助^^

你好,如何监听下载进度 progress