5

I've written my code so far and can get a list of all the records to show up on a webpage, however I need to be able to get it as a CSV (comma separated values) file.

Right now the page shows a list like follows:

Name      Address      Description
Bob       1 street     Journalist
Bill      2 street     Fireman
etc...

Anyway I can have meteor create a CSV file for download, instead of it showing up as a webpage with all the HTML markup?

npderi
  • 79
  • 1
  • 6
  • possible duplicate of [How to serve a file using iron router or meteor itself?](http://stackoverflow.com/questions/21565991/how-to-serve-a-file-using-iron-router-or-meteor-itself) – David Weldon Dec 01 '14 at 22:41

1 Answers1

8

Based on How to serve a file using iron router or meteor itself?

HTML:

<template name="blah">
  <a href="{{pathFor 'csv'}}">Download the CSV</a>
</template>

JS:

// An example collection
var DummyData = new Mongo.Collection("dummyData");

// create some sample data
if (Meteor.isServer) {
  Meteor.startup(function() {
    var dummyDataCursor = DummyData.find();
    if (dummyDataCursor.count() === 0) {
      for(var i=1; i<=100; i++) {
        DummyData.insert({Name: "Name" + i,Address: "Address" + i, Description:"Description" + i});
      }
    }
  });
}

Router.route('/csv', {
  where: 'server',
  action: function () {
    var filename = 'meteor_dummydata.csv';
    var fileData = "";

    var headers = {
      'Content-type': 'text/csv',
      'Content-Disposition': "attachment; filename=" + filename
    };
    var records = DummyData.find();
    // build a CSV string. Oversimplified. You'd have to escape quotes and commas.
    records.forEach(function(rec) {
      fileData += rec.Name + "," + rec.Address + "," + rec.Description + "\r\n";
    });
    this.response.writeHead(200, headers);
    return this.response.end(fileData);
  }
});
Community
  • 1
  • 1
cobberboy
  • 5,598
  • 2
  • 25
  • 22
  • I should have noted: this requires iron router. ```meteor add iron:router``` . – cobberboy Dec 02 '14 at 05:19
  • Ok, nice. Didn't realize I could use Iron Router like that. Thank you cobberboy, that was very helpful. I'll try that out, and will post back to give an update. – npderi Dec 02 '14 at 21:57
  • This is great and works great too. However what I'm running up against now is that when I click the button the routes me to my '/csv' server-side route, the template that has the button on it gets destroyed and my view is lost? Is there a way to retain all of the loaded templates, AND not having them get destroyed and still run the '/csv' route on the server? Either something like a prevent template destroy method or Router.go() at the conclusion of the server-side CSV route...or something...? – WizzyBoom Mar 10 '15 at 16:40
  • @WizzyBoom have you tried adding a target="_blank" attribute to the element? i.e.: Download the CSV – cobberboy Mar 12 '15 at 02:32
  • 1
    @cobberboy Thanks! I was able to solve my situation by making the link a button then adding a Template event `'click .btnExportCSVFormat' : function(e) { window.location.href = '/export-csv/' + $(e.target).attr('data-list-id'); }` this triggered the download without doing anything to the views or popping open a new window or tab to have to close after the download was complete. There maybe a cleaner way but this serves me well for now. Thanks!! – WizzyBoom Mar 12 '15 at 16:08