使用快速js请求模块下载文件

时间:2016-09-10 14:30:47

标签: node.js express request downloading

我正在下面的代码中下载文件......

var download = function(uri, filename, callback, path) {

  request
  .get(uri)
  .on('response', function (response) {
    var responseType = (response.headers['content-type'] || '').split(';')[0].trim();
    var ext = mime.extension(responseType);

    filename += '.' + ext;

    var fileStream = fs.createWriteStream(filename)
                    .on('finish', function () {

                    });

    this.pipe(fileStream);

    path = filename; //this doesn't work, the path by reference gets empty

  });

  //I've tried setting a variable here with the file name's value and 
  //returning it here but it's returning empty value

};

一切正常但我需要一种方法将文件名返回给函数调用者,但是它是空的,我不能通过引用在请求函数中使用这个文件名值设置一个变量..可能有人帮忙我?提前谢谢

0 个答案:

没有答案