Reputation: 185
I created a function which allows me to download a PDF, but it is not working.
Here is my function:
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);
});
}
My link
<a href="/downloadPDF">Download here</a>
And my route
router.get('/downloadPDF',isAuth, feedController.getDownloadPdf);
I expected to download the pdf, but it throws me
TypeError: res.send is not a function
If you can help me, thank you!
Upvotes: 0
Views: 21
Reputation: 1568
You can use Express res.download helper:
app.get('/downloadPDF', function(req, res){
const invoiceName = 'fw9' + '.pdf';
const invoicePath = path.join('data', 'invoices', invoiceName);
res.download(invoicePath);
});
Let me know if that helped you solve this
Upvotes: 1