如何在meteor.js中处理条件文件下载

Kri*_*r K 4 node.js meteor

在php中,你可以使用标题来强制下载文件,也可以隐藏实际的文件位置等.

如果您只希望特定条件下的某些用户能够下载某些文件,这将非常有用.

我怎么能在流星中这样做?我玩过Node.js fs模块,并设法在客户端上检索文件的二进制版本.但是,我如何将其转换为下载的实际文件?

谢谢!

Mic*_*oon 7

三个简单的步骤:

  1. 使用陨石
  2. 添加铁路由器包:mrt add iron-router
  3. 创建服务器方法来提供文件.这是一个例子:

    Router.map(function () {
      this.route('get-image', {
         where: 'server',
         path: '/img',
         action: function () {
            console.log('retrieving ' + this.request.query.name);
            this.response.writeHead(200, {'Content-type': 'image/png'}, this.request.query.name);
            this.response.end(fs.readFileSync(uploadPath + this.request.query.name));
        }
      });
    });
    
    Run Code Online (Sandbox Code Playgroud)

在此示例中,请求是HTTP GET带有一个参数的请求name=name-of-pdf.pdf.

这就是全部.希望这是你想要的.