Express.js中的res.download文件中发生了什么?
我正在创build一个在服务器上创buildPDF文档的应用程序,然后显示一个“ Download Here
button。 当我点击button,过程似乎工作。 当我检查Chrome控制台中的networking>预览和networking>标题标签时,我可以看到该文件已经返回。
问题是,它不显示,也不提供保存选项。 我错过了客户端的一步吗? 我的首选结果是让用户select保存文件或自动开始下载到他们的默认path。
这里是相关的服务器代码:
exports.show = function(req, res) { var file = req.params.id; var filePath = __dirname + '../../../lib/completedforms/'; var thisPath = path.resolve(filePath + file); res.attachment(thisPath); res.setHeader('Content-Type', 'application/pdf'); res.setHeader("Content-Disposition", "attachment"); res.download(thisPath); };
在此先感谢您的任何指导。
不需要res.attachment()
和 res.download()
。 只要使用后者。
此外, res.download()
已经设置了Content-Disposition
标题,所以你也可以删除它。
你也可以简化你的path生成:
var thisPath = path.resolve(__dirname, '../../../lib/completedforms/', file);
尽pipe你可能应该清理file
和/或检查这个thisPath
不是一些不应该在的位置。 这将防止某人提供像../../../../../../../etc/passwd
这样的潜在恶意的req.params.id
值。
- 我如何正确构build我的NodeJS,Express和MongoDB代码?
- 使用XMLHttpRequest将多个file upload到Express.js 3.5服务器
- Node.js Express.js保密的静态文件
- 同时访问两个远程资源并将其输出到合并的响应中 – NodeJS
- 无法将nodejs应用程序部署到heroku
- TypeError:文章不是函数,POST 500(内部服务器错误)
- express.js将api访问权限限制在仅来自同一网站的页面
- 我怎样才能创build一个dynamic辅助函数的数据库查询
- 隐藏/禁用字段从req.body消失(Express JS BodyParser)