node.js - 我想做一个下载部分
问题描述
我创建了一个允许我下载 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
如果你能帮助我,谢谢!
解决方案
您可以使用 Express res.download助手:
app.get('/downloadPDF', function(req, res){
const invoiceName = 'fw9' + '.pdf';
const invoicePath = path.join('data', 'invoices', invoiceName);
res.download(invoicePath);
});
让我知道这是否有助于您解决这个问题
推荐阅读
- java - 在 Java 中将拆分器转换为列表的最佳习语是什么?
- python - PyQuery 和 PDFQuery 从 XML 或 PDF 中的下一个兄弟中提取文本
- sharepoint - 远程服务器在 SSIS 工具上返回错误 404 共享点连接管理器
- java - 如何在 Android 10 上将文件保存到内部存储
- assembly - AVR 汇编为什么 LDI 指令只能使用 R16 - R31 寄存器
- c - 如何在 C 中的函数指针中注入 Shellcode?
- javascript - React 中的这个 handleClick 函数如何检索先前的状态值?
- sql - 根据 SAS 中相似但不相等的列将两个表连接在一起
- linux - 带有可变结尾的 Sed 正则表达式匹配不匹配
- angular - 将 Zoho Chatbot 集成到 Angular 并在登录后显示