Meteor, generate and download file

后端 未结 1 799
孤独总比滥情好
孤独总比滥情好 2021-01-07 01:23

I am trying to generate a simple text file from a meteor collection. I would like the user to click on a button (let\'s say \'Convert to text file\' button) and he would be

相关标签:
1条回答
  • 2021-01-07 01:42

    If using Iron Router, add a route on the server that generates the text file and set the appropriate headers and end the response with the file generated:

    Router.map(function() {
      this.route('txtFile', {
        where: 'server',
        path: '/text',
        action: function() {
          var text = "This is the awesome text.";
          var filename = 'textfile' + '.txt';
    
          var headers = {
            'Content-Type': 'text/plain',
            'Content-Disposition': "attachment; filename=" + filename
          };
    
          this.response.writeHead(200, headers);
          return this.response.end(text);
        }
      })
    });
    

    And on the client:

    <a href="/text">Download text</a>
    
    0 讨论(0)
提交回复
热议问题