【问题标题】:Force links to MP4 files to download instead of opeaning in browser using Meteor.js强制下载 MP4 文件的链接,而不是使用 Meteor.js 在浏览器中打开
【发布时间】:2015-07-12 09:18:49
【问题描述】:
我已经提取了指向 youtube 文件的直接链接,单击链接后需要开始下载。我使用 HTML5 download="filename",但它在 IE 或 Opera 中不起作用。我已经看到了在 PHP 中添加到文件 Header 的解决方案,但是如何使用 Meteor.js 或 Node.js 来完成。
编辑: Solution for Node.js,但仍然想知道 Meteor.js
<a href="http://example.com/file.mp4" download="filename">Link</a>
【问题讨论】:
标签:
javascript
node.js
meteor
download
【解决方案1】:
Consider using iron:router
在你做的服务器上:
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'});
【讨论】:
-
感谢您的回答,但我还是有点迷茫,因为我没有使用过 iron:router。我需要下载的文件实际上不是 video.mp4 之类的文件,而是指向该文件的直接链接 - youtube 上的 goo.gl/Z19ShH。那么在服务器方法中提取此链接后,我应该以链接作为参数调用Router.go吗?