Calin Onaca
Calin Onaca

Reputation: 185

I want to make a download section

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

Answers (1)

Tomer
Tomer

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

Related Questions