dart - 使用 Dart 动态创建用于下载的文本文件

标签 dart dart-html

我们如何在不涉及服务器的情况下使用 Dart 动态创建用于下载的文本文件?

最佳答案

此问题已被asked and answered for JS 。与该线程中的解决方案非常相似,只需很少的修改即可应用于 Dart。例如,我们可以使用自定义数据 URI 创建一个 anchor 元素,并将其呈现给用户或调用其 click 方法:

String encodedFileContents = Uri.encodeComponent("Hello World!");

new AnchorElement(href: "data:text/plain;charset=utf-8,$encodedFileContents")
  ..setAttribute("download", "file.txt")
  ..click();

参见DartPad example允许用户编辑表格元素的单元格,然后将单元格内容下载为 csv 文件。

关于dart - 使用 Dart 动态创建用于下载的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44082163/

相关文章:

dart - 使子小部件对其父背景透明

javascript - 在 MidiOutput 代理上调用 "send"时出现类型错误

dart - 如何在 Dart SDK 0.4+ 中使用 setInterval/setTimeout

ios - 发送Http.MultipartRequest作为 flutter 数组?

security - 在 Dart 中从浏览器向 websocket 连接添加额外的 header

flutter - 在 android studio 上从 "Pub get"获取错误

flutter - 错误 : Couldn't find constructor 'ImagePickerOptions' . ImagePickerOptions options = const ImagePickerOptions()

flutter - 将API(模型类)传递给小部件

flutter - 如何在 Flutter 中创建带有对象的下拉列表

dart - Dart SVG转换对象