Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uploading files through DriveApi.files.create() on web #74

Closed
ggeorgiev92 opened this issue Jun 6, 2019 · 3 comments
Closed

Uploading files through DriveApi.files.create() on web #74

ggeorgiev92 opened this issue Jun 6, 2019 · 3 comments
Labels

Comments

@ggeorgiev92
Copy link

ggeorgiev92 commented Jun 6, 2019

Is it possible to upload files from the WEB through googleapis DriveApi instance?
How to convert File() (dart instance) to media file which create method need?

I tried to read the file with FileReader() as readAsArrayBuffer or readAsDataUrl but in first case I'm receiving Uint8List, in second String (base64).

Part of my code:

final reader = new FileReader();
    reader.readAsDataUrl(file);
    reader.onLoad.listen((e) async{

      drive.File fileMetadata = drive.File.fromJson({
        "mimeType": file.type,
        "name": file.name,
        "parents": [folderId]
      });

      drive.Media fileMedia = drive.Media(reader.result, file.size, contentType: file.type);

      drive.DriveApi.files.create(fileMetadata, uploadMedia: fileMedia)

Issue here is that Media() want first parameter to be async.Stream<core.List<core.int>>;
Can someone give me a solution of creating a file from web with that google package?

pub dependencies: googleapis: ^0.52.0+1

@ggeorgiev92 ggeorgiev92 changed the title Uploading files through DriveApi.files.create() Uploading files through DriveApi.files.create() on web Jun 6, 2019
@jonasfj
Copy link
Member

jonasfj commented Jun 7, 2019

If you need to create a Stream<List<int>> from a String you can just do:

import 'dart:convert';

final Stream<List<int>> stream = () async * {
  yield utf8.encode('hello-world');
}();

This is creating an async generator and calling it...

Is that what you're looking for?

@ggeorgiev92
Copy link
Author

That's close to what I need.
How can I do that magic with a File? I have file object - do I need to read it with FileReader() and if yes how? I tried with replace encode parameter with reader.result but the file was with broken encoding after that. Any idea?

@jonasfj
Copy link
Member

jonasfj commented Jun 12, 2019

I think this part has to do with File from html:js, probably it's something a long the lines of:
https://stackoverflow.com/a/15981017/68333

But I think that is outside the scope of this package.

@jonasfj jonasfj closed this as completed Jun 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants