我想知道如何将用户计算机中的图像选入我的Flutter Web应用程序中进行上传
我尝试下面的代码,它的工作。
第一 import 'dart:html';
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()
_startFilePicker()