如何挑选文件和图像以通过Flutter Web上传 [英] How to Pick files and Images for upload with flutter web

查看:697
本文介绍了如何挑选文件和图像以通过Flutter Web上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从用户计算机中选择图像到我的flutter Web应用程序中进行上传

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

推荐答案

I

首先 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);
      }
    });
    }

现在只有任何小部件,例如按钮,并调用方法 _startFilePicker()

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

这篇关于如何挑选文件和图像以通过Flutter Web上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆