Get files from Private folder

So I want to display some .pdf content, that can be only available in the private folder.

Im trying using assets, but it gets the content from the pdf, not the file.

Here some of the code:

This method on server

 getPdf : function(){

    var data = Assets.getText('someFile.pdf');
    return data;
}

this call on client:

Meteor.call('getPdf', function(error,result) {
   data = result;
});

I want the same functionality as files from /public folder, but in /private, something like this:

<a href="{{data}}" target="_blank" ></a>

Is it possible?

Thanks

You could do something like this (for this example I’m assuming you have a file called meteor.pdf in your private directory - and please note, this is a quick example; I’m skipping over proper error handling, etc.):

example.html:

<head>
  <title>private-pdf</title>
</head>

<body>
  <a href="#" class="js-download-pdf">Download PDF</a>
</body>

example.js:

if (Meteor.isClient) {
  Template.body.events({
    'click .js-download-pdf'(event) {
      event.preventDefault();
      Meteor.call('downloadPdf', (error, pdf) => {
        window.open(`data:application/pdf;base64, ${pdf}`);
      });
    }
  });
}

Meteor.methods({
  downloadPdf() {
    if (!this.isSimulation) {
      const rawPdf = Assets.getBinary('meteor.pdf');
      const jsonPdf = EJSON.toJSONValue(rawPdf);
      return jsonPdf['$binary'];
    }
  }
});
1 Like

Thank you so much,

it is just what I needed!

Works perfectly!