简体   繁体   English

在flutter web中上传图片

[英]Upload image in flutter web

Flutter web is still in technical preview but i want to pick an image from the disk and upload it to the server. Flutter web 仍处于技术预览阶段,但我想从磁盘中选择一个图像并将其上传到服务器。 Is there anyway to add HTML, JS to my flutter web project and interact with it?无论如何,是否可以将 HTML、JS 添加到我的 flutter web 项目中并与之交互?

You need addEventListener and also need to append it for woking it on mobile safari.您需要 addEventListener 并且还需要附加它以便在移动 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) => completer.completeError(error));
            return reader.onLoad.first.then((_) => reader.result as String);
        });

        final results = await Future.wait(resultsFutures);
        completer.complete(results);
    });
    // need to append on mobile safari
    document.body.append(uploadInput);
    final List<String> images = await completer.future;
    setState(() {
        _uploadedImages = images;
    });
    uploadInput.remove();
}

This also works:这也有效:

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) => completer.completeError(error));
        return reader.onLoad.first.then((_) => reader.result as String);
      });
      final results = await Future.wait(resultsFutures);
      completer.complete(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();
}

This will do what you want for web.这将做你想要的网络。 Just call it from any button and it will open the system dialog to chose the file.只需从任何按钮调用它,它就会打开系统对话框来选择文件。

import 'dart:typed_data';
import 'package:universal_html/prefer_sdk/html.dart';    
import '../../../providers/form_provider.dart';

Uint8List uploadedImage;

startFilePickerWeb(ProviderForm formProvider) 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) {
      //Here I send the imatge to my Provider 
        formProvider.setLogoEmpresa(reader.result);
      });

      reader.onError.listen((fileEvent) {
        print("Some Error occured while reading the file");
      });

      reader.readAsArrayBuffer(file);
    }
  });
}

The provider:提供者:

class ProviderForm extends ChangeNotifier {
    Uint8List logoEmpresa;
    void setLogoEmpresa(Uint8List newFile) {
       logoEmpresa = newFile;
       notifyListeners();
    }
}

Then in your view you can render it like this, in case no imatge it show a attachement icon:然后在您的视图中,您可以像这样渲染它,以防它显示附件图标:

Container(
      width: formProvider.logoEmpresa == null
          ? 70.0
          : 90.0,
      child: formProvider.logoEmpresa == null
          ? Icon(
              Icons.attach_file,
              size: 70.0,
              color: kColorPrincipal,
            )
          : Image.memory(formProvider.logoEmpresa,
              fit: BoxFit.fitWidth),
    ),

If you have any question, please ask and I'll do my best to help.如果您有任何问题,请提出,我会尽力提供帮助。

You can use image_picker_web :您可以使用image_picker_web

dependencies:
  image_picker_web: ^1.0.9

picks Images (as Widget, File or Uint8List) and Videos (as File or Uint8List)选择图像(作为小部件、文件或 Uint8List)和视频(作为文件或 Uint8List)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将图像上传到 web (firestore) - Flutter web - How to upload image to web (firestore) - Flutter web 使用 dio formdata 上传图片 flutter web - Upload image flutter web with dio formdata 将图像/文件上传到 Strapi (Flutter Web) - Upload Image/File to Strapi (Flutter Web) 如何将 memory 中的图像作为二进制图像上传到 Flutter web 中的主体上? - How to upload an image in memory, as a binary image on the body in Flutter web? 无法从颤振网络将图像上传到 Firebase 存储 - Can't upload image to Firebase Storage from flutter web 在universal_io上使用Flutter Web 问题上传图像 Supabase Storage - Upload image Supabase Storage with Flutter Web issue on universal_io 上传从 Flutter web 到 firebase 存储和 Firestore 的图像 - Upload image picked from Flutter web to firebase storage and to Firestore 我想在 Flutter web 应用程序中将图像作为文件类型上传 - I want to upload an Image as a File type in Flutter web app 如何使用 flutter web 将图像直接上传到 s3 存储桶? - How to upload image directly to s3 bucket using flutter web? 如何将图像文件上传到Firebase存储并在Flutter web app中显示 - How to upload an image file to Firebase Storage and show it in the Flutter web app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM