在Flutter网站中上传图片

编程入门 行业动态 更新时间:2024-10-11 01:19:23
本文介绍了在Flutter网站中上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Flutter Web仍处于技术预览中,但是我想从磁盘中选择一个图像并将其上传到服务器。无论如何,有没有将HTML,JS添加到我的Flutter网站项目中并与其交互?

Flutter web is still in technical preview but i want to pick an image from the disk and upload it to the server. Is there anyway to add HTML, JS to my flutter web project and interact with it?

推荐答案

您需要addEventListener并且还需要附加它以在移动浏览器中将其唤醒。我也在在这里回答。

You need addEventListener and also need to append it for woking it on mobile safari. I answered here too.

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) => completerpleteError(error)); return reader.onLoad.first.then((_) => reader.result as String); }); final results = await Future.wait(resultsFutures); completerplete(results); }); // need to append on mobile safari document.body.append(uploadInput); final List<String> images = await completer.future; setState(() { _uploadedImages = images; }); uploadInput.remove(); }

这也有效:

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 on 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) => completerpleteError(error)); return reader.onLoad.first.then((_) => reader.result as String); }); final results = await Future.wait(resultsFutures); completerplete(results); }); // need to append on mobile safari document.body.append(input); // input.click(); can be here final List<String> images = await completer.future; setState(() { _uploadedImages = images; }); input.remove(); }

更多推荐

在Flutter网站中上传图片

本文发布于:2023-10-10 09:40:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1478314.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传图片   网站   Flutter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!