使用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