强制下载JavaScript中的浏览器(客户端网站)中的url的YouTubevideo

我正在使用基于node的服务器站点的YouTubevideo下载网站。 我正在使用叫做ytdl-core npm包,这个模块正在返回下载链接。 但是,当我点击该链接时,它将我redirect到“video预览”,而不是“触发器下载”,

 ytdl(url, function(err, format) { if (err) {res.send('the url is invalid please try again...');} console.log((format.formats).length);//i am getting value... in console but not outside of the function.. //this object contains all the download links var links = [] for( var i= 0, len = (format.formats).length; i < len; i++) { var el = format.formats[i].container; if ( el === 'mp4') { var download_url = format.formats[i]; //this push will store the all links in 'links' object.. links.push(download_url); } } console.log(links); }); 

所以当我把链接粘贴到我的浏览器时,它会redirect到预览而不是触发器下载。 我得到的链接如下所示:

 https://r8---sn-gxap5ojx-cage.googlevideo.com/videoplayback?upn=P6gEaXLtqOc&ei=ROrcWMzGKsjfoAPtl6XYAg&id=o-AIBZRxwo9AvJU4q02_xR0lfxJbSV3pxpga67Si4bLieu&key=yt6&ip=139.59.64.12&ipbits=0&pl=20&requiressl=yes&initcwndbps=8882500&ratebypass=yes&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpcm2cms%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cupn%2Cexpire&itag=18&expire=1490894500&mime=video%2Fmp4&pcm2cms=yes&source=youtube&mv=m&mt=1490872775&ms=au&lmt=1487309879666192&clen=8449602&gir=yes&mn=sn-gxap5ojx-cage&mm=31&dur=221.518&signature=6ED71F39CAA3B6719A8BAAEABD51D568ABF4D7A3.B3EED034140574BAC9733EC5CC28EF4AC9E3F758&title=Kygo Selena Gomez It Ain t Me with Selena Gomez Audio 

我的问题是,如何通过单击button强制下载该video到客户端的浏览器?

如果使用express,则可以使用res.download() 。 这将符合这一点。

 res.download(downloadLink, downloadTitle, function(err){ if (err) { // Handle error, but keep in mind the response may be partially-sent // so check res.headersSent } else { // decrement a download credit, etc. } }); 

祝你好运! 希望这可以帮助!

我发现很简单的解决scheme是,你需要添加属性"download='true'到你的HTML代码!它看起来像这样

 <a href='yourdownloadlink.com/example' download='true'>force download!</a>