Express.js – 如何将base64string下载为PDF文件?
我有PDF文件编码为base64string。 如何以.pdf格式将此string下载到浏览器中?
我已经试过了:
res.set('Content-Disposition', 'attachment; filename="filename.pdf"'); res.set('Content-Type', 'application/pdf'); res.write(fileBase64String, 'base64');
我最终解码PDF,然后将其作为二进制发送到浏览器,如下所示:
(为了简单起见,我在这里使用node-http
,但函数也可以用express
)
const http = require('http'); http.createServer(function (req, res) { getEncodedPDF(function (encodedPDF) { res.writeHead(200, { 'Content-Type': 'application/pdf', 'Content-Disposition': 'attachment; filename="filename.pdf" }); const download = Buffer.from(encodedPDF.toString('utf-8'), 'base64'); res.end(download); }); }).listen(1337);
让我疯狂的是邮差testing:
我使用发送button而不是发送和下载 button来提交请求:
但是使用这个请求的发送button会导致pdf文件在保存之后被破坏。
- 从express 3.x升级到express 4.x – Mongo会话存储
- 对于Node.js应用程序,使用带有Express-Handlebars的Angular
- Heroku应用程序使用generator-angular-fullstack失败构build
- 从node.js返回文件的parsing输出
- 如何在不重新启动应用程序的情况下为所有用户清除快速会话?
- node.js,以同步的方式在循环中一个接一个地执行mysql查询
- 如何使用ExpressJS / Connect / Crypto获取(大)文件的校验和?
- 处理mongodb时直接stream式传输错误来表示响应
- 如何通过sudo在生产模式下运行node.js Express?