首页 > 解决方案 > 我想做一个下载部分

问题描述

我创建了一个允许我下载 PDF 的功能,但它不起作用。

这是我的功能:

exports.getDownloadPdf =  (res,req,next) => {
const invoiceName = 'fw9' + '.pdf';
const invoicePath = path.join('data', 'invoices', invoiceName);
fs.readFile(invoicePath, (err, data) => {
if(err) {
 return next(err);
}
res.send(data);
});
}

我的链接

 <a href="/downloadPDF">Download here</a>

还有我的路线

router.get('/downloadPDF',isAuth, feedController.getDownloadPdf);

我希望下载pdf,但它让我失望

TypeError: res.send is not a function

如果你能帮助我,谢谢!

标签: node.js

解决方案


您可以使用 Express res.download助手:

app.get('/downloadPDF', function(req, res){
    const invoiceName = 'fw9' + '.pdf';
    const invoicePath = path.join('data', 'invoices', invoiceName);
    res.download(invoicePath);
});

让我知道这是否有助于您解决这个问题


推荐阅读