我需要生成 pdf 文档。关于 pdf 的所有魔力都在 generateDoc
函数中,该函数返回带有 Buffer 数据作为参数的 Promise。但 Express 不向客户端发送数据,仅向客户端发送 header 。我做错了什么?
app.get('/', function(req, res) {
generateDoc().then(function(data) {
res.set({
'Content-Type': 'application/pdf',
'Content-Length': data.length
});
res.end(data);
});
});
最佳答案
解决方案:
如果要从服务器返回 pdf,则必须对 res.end 使用 binary
参数。
generateDoc().then(function(data) {
res.set({
'Content-Type': 'application/pdf',
'Content-Length': data.length
});
res.end(data, 'binary');
}).fail(function (error) {
res.end(500, "Some error");
});
关于javascript - 如何在 ExpressJS 中使用 Q Promise?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19658350/