Dart Html - 将 Blob 转换为文件

标签 dart dart-html

我正在尝试为一些 dart:html 代码编写测试。

我有一个带有文件参数的方法(html 文件,而不是 io 文件)。

testFile(File file)

我能够使用文件所需的数据(减去文件名、日期等)创建一个 Blob,但似乎无法在 dart:html 中创建 File 对象,因为它是为内部保留的在 html_dartium.dart 中使用。

factory File._() { throw new UnsupportedError("Not supported"); }

还有其他方法来创建 HTML 文件对象吗?

我见过提到的 FileReaders,但这些结果要么是 String 要么是 uint8list。

最佳答案

经过进一步研究,我通过以下方式实现了我的目标:

  List<String> file_contents = ["test\n"];
  Blob blob = new Blob(file_contents, 'text/plain', 'native');

  FileSystem _filesystem = await window.requestFileSystem(1024 * 1024, persistent: false);
  FileEntry fileEntry = await _filesystem.root.createFile('dart_test.csv');
  FileWriter fw = await fileEntry.createWriter();
  fw.write(blob);
  File file = await fileEntry.file();

关于Dart Html - 将 Blob 转换为文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35164170/

相关文章:

flutter - 当我按下下载按钮时,如何显示带有CircularProgressIndicator的弹出窗口和下载状态

flutter - 如何在 Flutter 的 iOS 和 Android(原生)上显示不同的日期选择器?

dart - 加载应用程序时,如何禁止 RegistrationPage 短暂出现/闪烁?

flutter - 如何从 List<Map<String, dynamic>> 在 Flutter 中获取值

android - 未找到 : 'dart:html' import 'dart:html' ; I don't need dart:html and I also didn't used, 但我尝试通过导入它,但错误不会发生

dart - 如何在类中使用querySelector?

forms - flutter - 当键盘出现时,堆栈上的文本如何不起来?

带有多部分/表单数据的 DART 帖子

Dart:无需重新加载的动态重定向

authentication - Dart 登录/注销示例