防爆preSS - 返回二进制数据 [英] Express - Return binary data

查看:212
本文介绍了防爆preSS - 返回二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用防爆preSS返回一些二进制数据。在这个例子中,它是一个PDF但theorically,这可以是任何类型的文件的

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.

不过,专注于PDF的时刻。我写这篇code:

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);
        }
    );
});

根据我所看到的在这里: https://github.com/strongloop / EX preSS /问题/ 1555

Based on what I saw here : https://github.com/strongloop/express/issues/1555

不过,我得到的网页,对所有权权利号的PDF文件....但所有的页面都是空白的。

But, i get a pdf file with the right number of pages, right title.... but all the pages are blank

我敢肯定,关注getBinaryData()返回,因为此功能要求外部Web服务,当我直接问这个服务,我得到了正确的文件。

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.

感谢您提前为你的答案

推荐答案

我发现了一个更简单的解决方案:

I found a more simple solution :

request(req.url).pipe(res);

这条管道从遥远的Web服务的原始响应直接到我的反应!我得到了正确的文件,因此无论文件类型的

This pipe the original response from distant Web Service directly to my response ! I got the correct file and so regardless of filetype

这篇关于防爆preSS - 返回二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆