[SOLVED] Generate and Download file in the client side

Is there any way to generate a csv file and download it in the client side using meteor? I would the user to be able to click a button and then a file would be downloaded without leaving the current page.

Template.analytics.events({
  'click .csv-export' (event, template) {
    ....code to generate csv
    ....download file
  }
});

How did you solve it?

In my app where I need similar thing (just with json file), I have one button which sends request to the server to gather the needed data. When the data is received from server I display another button like this:

<a
  href={`data:text/json;charset=utf-8,${encodeURIComponent(JSON.stringify(data))}`}
  download="data.json"
  type="application/json"
>
  Download your data!
</a>

The link utilizes the download attribute while encoding the data in the href attribute. As a result once the user clicks the link they will be prompted to download a file with the data that was set it.
Iā€™m sure this can be easily adapted for any other types of files.
Hope this helps.

Thank you for showing how you did this, it has made my life so much easier!

1 Like