使用node,实现文件的下载功能


// 文件下载  将pdf文件转为流的形式,传到前端,实现下载功能
router.get('/downloadFile',(req,res)=>{
    let { id } = req.query;
    // var path="public/pdf/5ea56710fb5a2fdad7cabcf3.pdf";
    var path=`public/pdf/${id}.pdf`;
    var fileStream = fs.createReadStream(path);
    res.setHeader('Content-type', 'application/octet-stream');
    // res.setHeader('Content-Disposition', 'attachment;filename=1.pdf'); 
    res.setHeader('Content-Disposition', `'attachment;filename=${id}.pdf'`); 
    fileStream.on('data', function (data) {
        res.write(data, 'binary');
    });
    fileStream.on('end', function () {
        res.end();
        console.log('The file has been downloaded successfully!');
    });
})

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM