在Flutter网站中上传图片

时间:2019-07-16 18:24:51

标签: flutter dart

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

3 个答案:

答案 0 :(得分:0)

这将完成您想要的网络操作。 只需从任何按钮调用它,它将打开系统对话框以选择文件。

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

提供者:

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

然后在您的视图中可以这样渲染,以防万一没有图像显示附件图标:

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),
    ),

如果您有任何疑问,请询问,我们将尽力为您提供帮助。

答案 1 :(得分:0)

您需要addEventListener,还需要附加它以便在移动浏览器中将其唤醒。我回答了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();
}

这也可以:

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();
}

答案 2 :(得分:0)

您可以使用 image_picker_web

dependencies:
  image_picker_web: ^1.0.9

拾取图像(作为小部件,文件或Uint8List)和视频(作为文件或Uint8List)