使用 Dart 动态创建用于下载的文本文件
Dynamically create a text file for download using Dart
我们如何在不涉及服务器的情况下使用 Dart 动态创建文本文件供下载?
这个问题已经asked and answered for JS了。与该线程中的解决方案非常相似,只需很少的修改即可适用于 Dart。例如,我们可以创建一个带有自定义数据 URI 的锚元素,然后将其呈现给用户或调用其 click
方法:
String encodedFileContents = Uri.encodeComponent("Hello World!");
new AnchorElement(href: "data:text/plain;charset=utf-8,$encodedFileContents")
..setAttribute("download", "file.txt")
..click();
参见 DartPad example,它允许用户编辑 table 元素的单元格,然后将单元格内容下载为 csv 文件。
我们如何在不涉及服务器的情况下使用 Dart 动态创建文本文件供下载?
这个问题已经asked and answered for JS了。与该线程中的解决方案非常相似,只需很少的修改即可适用于 Dart。例如,我们可以创建一个带有自定义数据 URI 的锚元素,然后将其呈现给用户或调用其 click
方法:
String encodedFileContents = Uri.encodeComponent("Hello World!");
new AnchorElement(href: "data:text/plain;charset=utf-8,$encodedFileContents")
..setAttribute("download", "file.txt")
..click();
参见 DartPad example,它允许用户编辑 table 元素的单元格,然后将单元格内容下载为 csv 文件。