我们如何在不涉及服务器的情况下使用 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/