使用 Flutter web (chrome) 从系统目录获取文件路径以读取文件内容 例如:CSV 或文本文件

Get file path from system directory using Flutter web (chrome) to read file content Eg: CSV or Text file

尝试过的包:https://pub.dev/packages/file_picker

尝试了通过 GitHub 共享的示例代码实现。但是文件路径被 returned 为 null for 网络平台。移动平台中的相同实现 return 路径符合预期。

https://github.com/miguelpruivo/flutter_file_picker/blob/master/example/lib/src/file_picker_demo.dart

知道的事情:

  1. Path_Provider - 不支持网页
  2. dart-io-library - 不支持网页
  3. Flutter Repo
  4. 中打开问题

目标是从路径读取文件而不是上传。实现此目的的任何解决方法都会有所帮助。

Flutter 频道:测试版

我有一个从电脑上取图片的功能。可能对你有用。


import 'dart:html';

void uploadImage({@required Function(File file) onSelected}) {
  InputElement uploadInput = FileUploadInputElement()..accept = 'image/*';
  uploadInput.click();

  uploadInput.onChange.listen((event) {
    final file = uploadInput.files.first;
    final reader = FileReader();
    reader.readAsDataUrl(file);
    reader.onLoadEnd.listen((event) {
      onSelected(file);
    });
  });
}

file_picker中所述FAQ:

Paths aren't inaccessible from browsers since those provide fake paths. If you want to create a File instance to upload it somewhere, like FireStorage, you can do so with the bytes directly.

final result = await FilePicker.platform.pickFiles(type: FileType.any, allowMultiple: false);

if (result.files.first != null){
  var fileBytes = result.files.first.bytes;
  var fileName = result.files.first.name;
  print(String.fromCharCodes(fileBytes));
}