2015-07-11 62 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

相關問題