Richard Ambler
Richard Ambler

Reputation: 5040

Dynamically create a text file for download using Dart

How can we dynamically create a text file for download using Dart without involving the server?

Upvotes: 2

Views: 749

Answers (1)

Richard Ambler
Richard Ambler

Reputation: 5040

This question has already been asked and answered for JS. Very similar solutions to those in that thread apply to Dart with very little modification. For example we could create an anchor element with a custom data URI and either present it to the user or call its click method:

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

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

See DartPad example that allows the user to edit a table element's cells and then download the cell contents as a csv file.

Upvotes: 4

Related Questions