Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Meteor.js强制下载指向MP4文件的链接,而不是在浏览器中运行_Javascript_Node.js_Meteor_Download - Fatal编程技术网

Javascript 使用Meteor.js强制下载指向MP4文件的链接,而不是在浏览器中运行

Javascript 使用Meteor.js强制下载指向MP4文件的链接,而不是在浏览器中运行,javascript,node.js,meteor,download,Javascript,Node.js,Meteor,Download,我已提取到youtube文件的直接链接,需要在单击链接后开始下载。我使用HTML5 download=“filename”,但它在IE或Opera中不起作用。我见过在PHP中添加到文件头的解决方案,但如何使用Meteor.js或Node.js实现呢 编辑:,但仍然对Meteor.js感到好奇 <a href="http://example.com/file.mp4" download="filename">Link</a> 在服务器上,您可以执行以下操作: var

我已提取到youtube文件的直接链接,需要在单击链接后开始下载。我使用HTML5 download=“filename”,但它在IE或Opera中不起作用。我见过在PHP中添加到文件头的解决方案,但如何使用Meteor.js或Node.js实现呢

编辑:,但仍然对Meteor.js感到好奇

<a href="http://example.com/file.mp4" download="filename">Link</a>

在服务器上,您可以执行以下操作:

var fs = Npm.require('fs');

Router.route('/files/:fileName', function() {

  var file = this.params.fileName;

  // Attempt to read the file size
  var stat = null;
  try {
    stat = fs.statSync(file);
  } catch (_error) {
    console.log(this.response);
  }

  // Set the headers
  this.response.writeHead(200, {
    'Content-Type': 'application/zip',
    'Content-Disposition': 'attachment; filename=' + file
    'Content-Length': stat.size
  });

  // Pipe the file contents to the response
  fs.createReadStream(file).pipe(this.response);
},
{where: 'server'});

谢谢你的回答,但我还是有点不知所措,因为我还没有用铁:路由器那么多。我需要下载的文件实际上不是像video.mp4这样的文件,而是直接链接到youtube上的文件。所以,在我在服务器方法中提取这个链接之后,我是否应该调用Router.go以链接作为参数?