I have some data on the client web browser (e.g. a string from a textarea) in my Dart web application. I would like to allow the User to download the text as a file. How can I do this? I do not want to use server side code.
Client side download can be performed with the following code:
void downloadFileToClient(String filename, String text){
AnchorElement tl = document.createElement('a');
tl..attributes['href'] = 'data:text/plain;charset=utf-8,' + Uri.encodeComponent(text)
..attributes['download'] = filename
..click();
}
This depends on browser support for the download attribute on anchor tags. Check your target platform for suitability.
The CanIUse site gives a status across browser platforms and versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With