流星,生成和下载文件 [英] Meteor, generate and download file

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

问题描述

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

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>

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆