How to Pick files and Images for upload with flutter web

后端 未结 2 2049
失恋的感觉
失恋的感觉 2020-12-15 21:45

I would like to know how to pick an Image from the users computer into my flutter web app for upload

2条回答
  •  不思量自难忘°
    2020-12-15 22:00

    I've tested this package and was very happy with the result imagePickerWeb it returns 3 different types it can be in the form of Image(widget for preview), byte, File(upload)

    then you can use this to get the values

    html.File _cloudFile;
     var _fileBytes;
     Image _imageWidget;
     
     Future getMultipleImageInfos() async {
        var mediaData = await ImagePickerWeb.getImageInfo;
        String mimeType = mime(Path.basename(mediaData.fileName));
        html.File mediaFile =
            new html.File(mediaData.data, mediaData.fileName, {'type': mimeType});
    
        if (mediaFile != null) {
          setState(() {
            _cloudFile = mediaFile;
            _fileBytes = mediaData.data;
            _imageWidget = Image.memory(mediaData.data);
          });
        }
    

    Uploading to firebase

    don't forget to add this to your index.html

      
    

    Uploading to firebase

    import 'package:firebase/firebase.dart' as fb;
        uploadToFirebase(File file) async {
    
        final filePath = 'temp/${DateTime.now()}.png';//path to save Storage
        try {
          fb
              .storage()
              .refFromURL('urlFromStorage')
              .child(filePath)
              .put(file);
        } catch (e) {
          print('error:$e');
        }
      }
    

    See the documentation of the package if you still have problems

提交回复
热议问题