从NodeJS服务器下载文件,添加用于内容处置的标头

时间:2015-09-08 02:12:04

标签: javascript node.js meteor http-headers

我有这个服务器功能,它将URL和文件名提取到保存在外部服务上的文件。

示例 - https://ec-media.sndcdn.com/ZtPlxyZHyzxy.128.mp3?f10880d39085a94a0418a7ef69b03d522cd6dfee9399eeb9a522059d69ffb9359bc7c231696809f40c29ddb8e488adfa7f7fccde067c557b3ae31730fc6901d18842314a30

我获取了变量的url和filename,但是如何设置标题'Content-disposition','attachment'; filename = filename;并将其返回给客户端,以便点击即可下载。

// load future from fibers
var Future = Meteor.npmRequire("fibers/future");
// load youtubedl
var youtubedl = Meteor.npmRequire('youtube-dl');
// load fs
var fs = Meteor.npmRequire('fs');
// require request
var request = Meteor.npmRequire('request');

Meteor.methods({
  'command' : function(line) {
    // this method call won't return immediately, it will wait for the
    // asynchronous code to finish, so we call unblock to allow this client
    this.unblock();
    var future = new Future();

    youtubedl.getInfo(line, function(err, stdout, stderr) {
      var url = stdout.url;
      var filename = stdout._filename;

      future.return({stdout: stdout, stderr: stderr});
    });
    return future.wait();
  }

});

0 个答案:

没有答案