33

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

2
  • Why don't use res.sendFile method? Commented May 13, 2015 at 11:38
  • 1
    cause I don't have the file : I get this binary content from another WebService. Eventually, I can create a temp file and use sendFile... Commented May 13, 2015 at 12:12

2 Answers 2

60

Here is my slightly cleaned up version of how to return binary files with Express. I assume that the data is in an object that can be declared as binary and has a length:

exports.download = function (data, filename, mimetype, res) {
    res.writeHead(200, {
        'Content-Type': mimetype,
        'Content-disposition': 'attachment;filename=' + filename,
        'Content-Length': data.length
    });
    res.end(Buffer.from(data, 'binary'));
};
Sign up to request clarification or add additional context in comments.

8 Comments

Slight typo. It should be: res.end(new Buffer(data), 'binary')
keep in mind that new Buffer() is deprecated and it have security issues.
@realtebo you can use Buffer.from() - see Node.js docs for more examples.
new Buffer() is deprecated, use Buffer.from() instead.
why use Buffer.from when data is binary, isn't that reserved for strings? wouldn't you have to use Buffer.alloc?
|
35

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.

4 Comments

I tried same thing but it's not working for me. stackoverflow.com/questions/37517312/…
In what library does request lib reside for request(req.url).pipe(res)?
For everyone wondering what library request comes from, it's a built in node module, so just add const request = require('request'); before the call.
Request seems to come from npmjs.com/package/request, which is deprecated now. github.com/request/request/issues/3142

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.