Skip to content

[image_picker] Add support for web #36281

Closed

Description

@OlfatMostafa

I know flutter web is still fresh but i'm trying to do image uploading in my flutter web project, so image_picker is still not supported but i'm looking for suggestions for any replacements or approaches for that.

I'm using firebase, and the part that i can't figure out is how to pick an image from my computer to upload it.

Thanks!

Activity

  1. ferhatb commented on Jul 16, 2019

    @ferhatb
    Contributor

    /cc @hterkelsen: would require hosting file input for upload and ability to click to run picker.

  2. added this to the milestone on Jul 18, 2019
  3. aabounegm commented on Jul 24, 2019

    @aabounegm

    You can try this code as a workaround until plugins are supported by Flutter Web. It worked with me.

    import 'dart:html';
    
    void main() {
      final InputElement input = document.createElement('input');
      input
          ..type = 'file'
          ..multiple = true
          ..accept = 'image/*';
      input.onChange.listen((e) {
        final List<File> files = input.files;
        for (final file in files) {
          final reader = new FileReader();
          reader.onLoad.listen((e2) {
            String fileContent = reader.result;
            print(fileContent);
          });
          reader.readAsText(file); // or '.readAsDataUrl' for using as img src, for example
        }
      });
      input.click();
    }

    Edit: here is a sample function that wraps it in a Future:

    import 'dart:html';
    import 'dart:async';
    
    Future<List<String>> getFiles() {
      final completer = new Completer<List<String>>();
      final InputElement input = document.createElement('input');
      input
          ..type = 'file'
          ..multiple = true
          ..accept = 'image/*';
      input.onChange.listen((e) async {
        final List<File> files = input.files;
        Iterable<Future<String>> resultsFutures = files.map((file) {
          final reader = new FileReader();
          reader.readAsDataUrl(file);
          reader.onError.listen((error) => completer.completeError(error));
          return reader.onLoad.first.then((_) => reader.result as String);
        });
        final results = await Future.wait(resultsFutures);
        completer.complete(results);
      });
      input.click();
      return completer.future;
    }

    or in case you want only one file:

    import 'dart:html';
    import 'dart:async';
    
    Future<String> getFile() {
      final completer = new Completer<String>();
      final InputElement input = document.createElement('input');
      input
          ..type = 'file'
          ..accept = 'image/*';
      input.onChange.listen((e) async {
        final List<File> files = input.files;
        final reader = new FileReader();
        reader.readAsDataUrl(files[0]);
        reader.onError.listen((error) => completer.completeError(error));
        await reader.onLoad.first;
        completer.complete(reader.result as String);
      });
      input.click();
      return completer.future;
    }

    Of course, you can tweak this according to your needs by changing the returned object (ex: File itself instead of reading it) and using a different reader.readAs* function.
    Seeing that almost all Firebase plugins for Flutter depend on the Flutter package, I don't think you can use the Flutter package. Try to see if you can use the Web one instead

  4. aggmoulik commented on Sep 11, 2019

    @aggmoulik

    I really love to work on the plugin.. Does anyone working on it?
    Please tell me how to start creating plugin.

  5. harryterkelsen commented on Sep 11, 2019

    @harryterkelsen
    Contributor

    @ditman has been working on a prototype for image_picker

  6. ditman commented on Sep 13, 2019

    @ditman
  7. Thathy916 commented on Oct 18, 2019

    @Thathy916

    Have you implemented the function of uploading the flutter web image? The image upload function I made can't be used on the mobile browser, except for chrome. What's the matter?

  8. changed the title [-]Image picker flutter web[/-] [+][image_picker] Add support for web[/+] on Nov 20, 2019
  9. ykaito21 commented on Dec 18, 2019

    @ykaito21

    Have you implemented the function of uploading the flutter web image? The image upload function I made can't be used on the mobile browser, except for chrome. What's the matter?

    I am facing the same issue. @Thathy916 Did you find any solution?

  10. ykaito21 commented on Dec 19, 2019

    @ykaito21

    I finally found the solution which probably works on mobile safari too. I leave it here. Hope it will help someone like me until image_picker adds support for web.

      Future<void> _setImage() async {
        final completer = Completer<List<String>>();
        InputElement uploadInput = FileUploadInputElement();
        uploadInput.multiple = true;
        uploadInput.accept = 'image/*';
        uploadInput.click();
        //* onChange doesn't work on mobile safari
        uploadInput.addEventListener('change', (e) async {
          // read file content as dataURL
          final files = uploadInput.files;
          Iterable<Future<String>> resultsFutures = files.map((file) {
            final reader = FileReader();
            reader.readAsDataUrl(file);
            reader.onError.listen((error) => completer.completeError(error));
            return reader.onLoad.first.then((_) => reader.result as String);
          });
    
          final results = await Future.wait(resultsFutures);
          completer.complete(results);
        });
        //* need to append on mobile safari
        document.body.append(uploadInput);
        final List<String> images = await completer.future;
        setState(() {
          _uploadedImages = images;
        });
        uploadInput.remove();
     }
    

    This also works:

      Future<void> _setImage() async {
        final completer = Completer<List<String>>();
        final InputElement input = document.createElement('input');
        input
          ..type = 'file'
          ..multiple = true
          ..accept = 'image/*';
        input.click();
        onChange doesn't work in mobile safari
        input.addEventListener('change', (e) async {
          final List<File> files = input.files;
          Iterable<Future<String>> resultsFutures = files.map((file) {
            final reader = FileReader();
            reader.readAsDataUrl(file);
            reader.onError.listen((error) => completer.completeError(error));
            return reader.onLoad.first.then((_) => reader.result as String);
          });
          final results = await Future.wait(resultsFutures);
          completer.complete(results);
        });
        document.body.append(input);
        // input.click(); can be here
        final List<String> images = await completer.future;
        setState(() {
          _uploadedImages = images;
        });
        input.remove();
      }
    
  11. 19 remaining items

  12. bytejutsu commented on Jun 1, 2020

    @bytejutsu
  13. ditman commented on Jun 1, 2020

    @ditman
    Member

    @DhiaEddineNabli sorry I hid your comment, that's not the package we're working on. The official package is going to be:

    image_picker_for_web

  14. ditman commented on Jun 4, 2020

    @ditman
    Member

    Published image_picker_for_web to pub.dev:

    https://pub.dev/packages/image_picker_for_web (^0.1.0)

    For now, it is not endorsed, so you'll have to depend both on image_picker and image_picker_for_web. Please, see the instructions in the README.

  15. ronytesler commented on Aug 21, 2020

    @ronytesler

    You can try this code as a workaround until plugins are supported by Flutter Web. It worked with me.

    import 'dart:html';
    
    void main() {
      final InputElement input = document.createElement('input');
      input
          ..type = 'file'
          ..multiple = true
          ..accept = 'image/*';
      input.onChange.listen((e) {
        final List<File> files = input.files;
        for (final file in files) {
          final reader = new FileReader();
          reader.onLoad.listen((e2) {
            String fileContent = reader.result;
            print(fileContent);
          });
          reader.readAsText(file); // or '.readAsDataUrl' for using as img src, for example
        }
      });
      input.click();
    }

    Edit: here is a sample function that wraps it in a Future:

    import 'dart:html';
    import 'dart:async';
    
    Future<List<String>> getFiles() {
      final completer = new Completer<List<String>>();
      final InputElement input = document.createElement('input');
      input
          ..type = 'file'
          ..multiple = true
          ..accept = 'image/*';
      input.onChange.listen((e) async {
        final List<File> files = input.files;
        Iterable<Future<String>> resultsFutures = files.map((file) {
          final reader = new FileReader();
          reader.readAsDataUrl(file);
          reader.onError.listen((error) => completer.completeError(error));
          return reader.onLoad.first.then((_) => reader.result as String);
        });
        final results = await Future.wait(resultsFutures);
        completer.complete(results);
      });
      input.click();
      return completer.future;
    }

    or in case you want only one file:

    import 'dart:html';
    import 'dart:async';
    
    Future<String> getFile() {
      final completer = new Completer<String>();
      final InputElement input = document.createElement('input');
      input
          ..type = 'file'
          ..accept = 'image/*';
      input.onChange.listen((e) async {
        final List<File> files = input.files;
        final reader = new FileReader();
        reader.readAsDataUrl(files[0]);
        reader.onError.listen((error) => completer.completeError(error));
        await reader.onLoad.first;
        completer.complete(reader.result as String);
      });
      input.click();
      return completer.future;
    }

    Of course, you can tweak this according to your needs by changing the returned object (ex: File itself instead of reading it) and using a different reader.readAs* function.
    Seeing that almost all Firebase plugins for Flutter depend on the Flutter package, I don't think you can use the Flutter package. Try to see if you can use the Web one instead

    I get this error on console:
    File chooser dialog can only be shown with a user activation.

  16. shinriyo commented on Sep 5, 2020

    @shinriyo
    Because image_picker_platform_interface >=1.1.0 depends on http ^0.12.1 and andu depends on http 0.12.0+4, image_picker_platform_interface >=1.1.0 is forbidden.
    
  17. shinriyo commented on Sep 5, 2020

    @shinriyo
    flutter clean
    flutter pub cache repair
    

    need?

  18. chaitanya-teja commented on Sep 24, 2020

    @chaitanya-teja

    You can try this code as a workaround until plugins are supported by Flutter Web. It worked with me.

    import 'dart:html';
    
    void main() {
      final InputElement input = document.createElement('input');
      input
          ..type = 'file'
          ..multiple = true
          ..accept = 'image/*';
      input.onChange.listen((e) {
        final List<File> files = input.files;
        for (final file in files) {
          final reader = new FileReader();
          reader.onLoad.listen((e2) {
            String fileContent = reader.result;
            print(fileContent);
          });
          reader.readAsText(file); // or '.readAsDataUrl' for using as img src, for example
        }
      });
      input.click();
    }

    Edit: here is a sample function that wraps it in a Future:

    import 'dart:html';
    import 'dart:async';
    
    Future<List<String>> getFiles() {
      final completer = new Completer<List<String>>();
      final InputElement input = document.createElement('input');
      input
          ..type = 'file'
          ..multiple = true
          ..accept = 'image/*';
      input.onChange.listen((e) async {
        final List<File> files = input.files;
        Iterable<Future<String>> resultsFutures = files.map((file) {
          final reader = new FileReader();
          reader.readAsDataUrl(file);
          reader.onError.listen((error) => completer.completeError(error));
          return reader.onLoad.first.then((_) => reader.result as String);
        });
        final results = await Future.wait(resultsFutures);
        completer.complete(results);
      });
      input.click();
      return completer.future;
    }

    or in case you want only one file:

    import 'dart:html';
    import 'dart:async';
    
    Future<String> getFile() {
      final completer = new Completer<String>();
      final InputElement input = document.createElement('input');
      input
          ..type = 'file'
          ..accept = 'image/*';
      input.onChange.listen((e) async {
        final List<File> files = input.files;
        final reader = new FileReader();
        reader.readAsDataUrl(files[0]);
        reader.onError.listen((error) => completer.completeError(error));
        await reader.onLoad.first;
        completer.complete(reader.result as String);
      });
      input.click();
      return completer.future;
    }

    Of course, you can tweak this according to your needs by changing the returned object (ex: File itself instead of reading it) and using a different reader.readAs* function.
    Seeing that almost all Firebase plugins for Flutter depend on the Flutter package, I don't think you can use the Flutter package. Try to see if you can use the Web one instead

    This solution works if we have to select a file. But when the file picker opens, and we cancel the dialog. This code won't run again. It keeps waiting for the file to be picked which was cancelled long back.

  19. cedvdb commented on Mar 18, 2021

    @cedvdb
    Contributor

    why not include it directly in the image picker package ?

  20. ditman commented on Mar 18, 2021

    @ditman
    Member

    image_picker default web support has been published recently.

    https://pub.dev/packages/image_picker/changelog#073

    image_picker has had web support (through an additional package: image_picker_for_web) since version 0.6.7+1. Check it out here: https://pub.dev/packages/image_picker_for_web

  21. pumuckelo commented on Mar 24, 2021

    @pumuckelo

    I got it working this way:

    // file is PickedFile received by ImagePicker

    var data = await file.readAsBytes();
    var uploadTask = FirebaseStorage.instance.ref().child("path").ref.putData(data);
    
  22. pumuckelo commented on Mar 24, 2021

    @pumuckelo

    Full working example function to use PickedFile with firebase storage

    Future<TaskSnapshot> uploadProfilePicture(
          String userId, PickedFile file) async {
       
        Reference ref = _storage
            .ref()
            .child("/users/$userId/images/profilePicture/profilePicture");
    
        if (isWeb()) {
          var data = await file.readAsBytes();
          return ref.putData(data);
        }
    
        return ref.putFile(File(file.path));
      }
    
  23. github-actions commented on Aug 4, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  24. locked as resolved and limited conversation to collaborators on Aug 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work listc: new featureNothing broken; request for a new capabilityp: image_pickerThe Image Picker plugin.packageflutter/packages repository. See also p: labels.platform-webWeb applications specifically

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions