本文介绍了流星,生成并下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从流星集合生成一个简单的文本文件.我希望用户单击一个按钮(例如,转换为文本文件"按钮),他将能够下载一个文本文件,其中包含将给定集合的元素转换为文本.

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 able to download a text file containing elements of a given collection converted to text.

我认为在服务器端生成http响应并修改http标头的内容类型可以,但是我不知道该如何实现.

I would think that generating a http response on the server side and modying the content type of the http header would do but I don't know how I can achieve that.

有人有建议吗?

推荐答案

如果使用Iron Router,请在服务器上添加一条生成文本文件的路由,并设置适当的标头,并以生成的文件结束响应:

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);
    }
  })
});

在客户端上:

<a href="/text">Download text</a>

这篇关于流星,生成并下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:54