NodeJs:通过Content-Diposition强制下载后,我得到0字节的txt文件吗?

时间:2019-07-08 12:05:10

标签: node.js http content-disposition

强制下载后我得到0字节的TXT文件,有时当我刷新它并单击相同的链接时,它可以正常工作。我有多个下载链接,它在不同的链接上随机显示此错误。但是刷新后效果很好。我尝试了所有技巧,但没有一个对我有用。

如果能为您提供帮助,我将不胜感激。

我正在尝试从googlevideos下载视频,当我检查链接时,它运行正常。

Link在工作,但是当我单击下载时,刷新后显示0字节TXT,单击相同的链接就可以了:( PS:链接正常。

`router.get('/download', function(req, res){
  var url = req.query.url;
  console.log(url);
  var filename = req.query.filename;
  https.get(url, function(response) {
            console.log(url);
            filename = filename.split('%20').join('_');
            //console.log(filename);
            //res.setHeader("Content-Length: ".strlen($data));
           // res.setHeader('Content-Type:' 'text/html; charset=UTF-8');
            res.setHeader('Content-disposition', 'attachment; filename='+filename);
            response.pipe(res);

    })`

JavaScript代码。

`<script>
function downloadVideo(url) {

        window.location = '/download?url=' + encodeURIComponent(url) + '&filename=' + encodeURIComponent('{{@root.meta.name}}');
}
</script>
 ` 

HTML文件代码

`<div class="column-box column-4">
  <a class="down-link" target="_blank" onclick="downloadVideo('{{url}}')" ><span class="glyphicon glyphicon-arrow-down"></span> Download </a>
  </div>
`

0 个答案:

没有答案