2015-07-11 415 views
0

我已经提取了直接链接到YouTube文件,并且需要在点击链接后开始下载。我使用HTML5下载=“文件名”,但它不适用于IE或Opera。我已经看到了在PHP中添加文件头的解决方案,但是如何使用Meteor.js或Node.js来完成。强制链接到MP4文件下载,而不是使用Meteor.js在浏览器中打开

编辑:Solution for Node.js,但还是想了解一下Meteor.js

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

回答

0

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'}); 
+0

谢谢回答,但我仍然是位丢失BC我没有使用铁:路由器那么多。我需要下载的文件实际上并不是像video.mp4这样的文件,而是直接链接到该文件 - YouTube上的https://goo.gl/Z19ShH。因此,在我在服务器方法中提取此链接后,是否应该将链接作为参数调用Router.go? – mhlavacka

相关问题