Express - 返回二进制数据 [英] Express - Return binary data
问题描述
但是现在关注pdf。我写了这段代码:
app.get('*',function(req,res){
getBinaryData req.url,
function(answer){
res.type('pdf');
res.end(new Buffer(answer,'binary'));
} ,
function(error){
res.setHeader('Content-Type','text / plain');
return res.end(error);
}
);
});
根据我在这里看到的内容: https://github.com/strongloop/express/issues/1555
但是,我得到一个pdf文件正确的页面,正确的标题....但所有的页面都是空白的
我确信关注getBinaryData()的返回,因为这个函数询问一个外部Web服务,当我直接问这个服务时,我得到了正确的文件。
提前谢谢你的答案
我发现一个更简单的解决方案:
请求(req.url).pipe(RES);
将远程Web服务的原始响应直接传递给我的回复!无论文件类型如何,我都获得了正确的文件。
I try to return some binary data with Express. In the example, it's a PDF but theorically, this can be any sort of file.
But focus on the pdf for the moment. I wrote this code :
app.get('*', function (req, res) {
getBinaryData(req.url,
function (answer) {
res.type('pdf');
res.end(new Buffer(answer, 'binary'));
},
function (error) {
res.setHeader('Content-Type', 'text/plain');
return res.end(error);
}
);
});
Based on what I saw here : https://github.com/strongloop/express/issues/1555
But, i get a pdf file with the right number of pages, right title.... but all the pages are blank
I'm sure concern the return of getBinaryData(), because this function asked an external Web Service and when I asked directly this service, I got the right document.
Thank you in advance for your answers
I found a more simple solution :
request(req.url).pipe(res);
This pipes the original response from distant Web Service directly to my response! I got the correct file regardless of the file type.
这篇关于Express - 返回二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!