The pdf file does not load correctly in the browser, but excellent with wget or curl with NodeJS / Express

I have an application in nodejs / express that creates a PDF file and passes it in response. When I execute a request through curl or wget, downloading files is just fine.

When I execute a request in a browser, the file does not load correctly. It is interesting that the file size is much larger (when downloading using wget / curl - 59 thousand, but when downloading in the browser - 101 thousand)

Here is the snippet that generates the response:

fs.readFile('file.pdf', function(err, data) {
  res.setHeader('Content-Disposition', 'attachment; filename=' + req.params.id + '.pdf');
  res.setHeader('Content-Type', 'application/pdf');                
  res.setHeader('Content-Length', data.length);
  res.status(200).end(data);
});

I don’t know if I’m missing a header or if there is something in the request that I should pay attention to, this is not so, but I have no idea why this is happening. Any ideas?

+4
source share
1

.

res.status(200).end(data);

res.status(200).end(data, 'binary');

, PDF .

, Express UTF-8 ( ).

Express.

0

Source: https://habr.com/ru/post/1568236/


All Articles