How to Pick files and Images for upload with flutter web

后端 未结 2 2053
失恋的感觉
失恋的感觉 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:12

    I tried the code below and it worked.

    first import 'dart:html';

    // variable to hold image to be displayed 
    
          Uint8List uploadedImage;
    
    //method to load image and update `uploadedImage`
    
    
        _startFilePicker() async {
        InputElement uploadInput = FileUploadInputElement();
        uploadInput.click();
    
        uploadInput.onChange.listen((e) {
          // read file content as dataURL
          final files = uploadInput.files;
          if (files.length == 1) {
            final file = files[0];
            FileReader reader =  FileReader();
    
            reader.onLoadEnd.listen((e) {
                        setState(() {
                          uploadedImage = reader.result;
                        });
            });
    
            reader.onError.listen((fileEvent) {
              setState(() {
                option1Text = "Some Error occured while reading the file";
              });
            });
    
            reader.readAsArrayBuffer(file);
          }
        });
        }
    

    now just any Widget, like a button and call the method _startFilePicker()

提交回复
热议问题