Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Download Web Client Side Data As A File Using The Dart Language

Tags:

dart

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.

like image 236
daftspaniel Avatar asked Oct 01 '13 18:10

daftspaniel


1 Answers

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.

like image 176
daftspaniel Avatar answered Nov 15 '22 09:11

daftspaniel