javascript - Creating a CSV file from a Meteor.js Collection -
i've written code far , can list of records show on webpage, need able csv (comma separated values) file.
right page shows list follows:
name      address      description bob       1 street     journalist bill      2 street     fireman etc...   anyway can have meteor create csv file download, instead of showing webpage html markup?
based on how serve file using iron router or meteor itself?
html:
<template name="blah">   <a href="{{pathfor 'csv'}}">download csv</a> </template>   js:
// example collection var dummydata = new mongo.collection("dummydata");  // create 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 csv string. oversimplified. you'd have escape quotes , commas.     records.foreach(function(rec) {       filedata += rec.name + "," + rec.address + "," + rec.description + "\r\n";     });     this.response.writehead(200, headers);     return this.response.end(filedata);   } });      
Comments
Post a Comment