Daniel Fischer
Daniel Fischer

Reputation: 3062

How to read a file and output to client in Meteor?

In meteor is it possible to click on a button on the client and then have that button output a files contents from the server?

The reason for this is based around documentation. I'd like to create a button that can copy and paste the html from a template file.

On the server side I'd need to read the file and then somehow pass that to the client to output. Is this possible?

Upvotes: 0

Views: 1401

Answers (1)

Kuba Wyrobek
Kuba Wyrobek

Reputation: 5273

Something like below should do the job:

Server side:

Meteor.methods({
  loadFile:function(path){
          var fs = Npm.require('fs');
          return fs.readFileSync(path, 'utf8');

  }
})

Client side:

Template.NAME.created = function(){
   this.file = new ReactiveVar("");
}

Template.NAME.helpers({
   file:function(){
       return Template.instance().file.get()
   }
})

Template.NAME.events({
   'click button':function(e,t){
       Meteor.call('loadFile','public/file.html', function(err, result)          {
         if(!err){
            t.file.set(result)
         }else{
            console.error('Error', err)
         }
       })
    }   
})

Upvotes: 1

Related Questions