0

I want to send a PDF file to the client, I to it with .sendFile from Express. I works good in Chrome, with a Download link chrome save it, with a normal _blank link, chrome open it in a new chrome tab.

But in Mozilla, Safari and IE, the Browser display a message "The file is damaged" or adobe reader. Adobe Reader doesn't show this message when the pdf was loaded over chrome.

My code:

app.get('/ebooks/:file', function(req, res, next){

  var options = {
    root: 'ebooks/',
    dotfiles: 'deny',
    headers: {
      'x-timestamp': Date.now(),
      'x-sent': true,
      'Content-Type': 'application/pdf'
    }
  };

  var fileName = req.params.file;
  res.sendFile(fileName, options, function (err) {
    if (err) {
      console.log(err);
      res.status(err.status).end();
    }
    else {
      console.log('Sent:', fileName);
    }
  });
});

Have anyone an idea why?

Varha

Varha
  • 11
  • 1
  • 5

1 Answers1

0

I had the same issue, but I changed to res.download and it worked fine. The behavior isn't as pretty in Chrome, but it gets the job done. Interested to see if someone else has had this experience with PDFs and has a good solution.

http://expressjs.com/4x/api.html#res.download

jstafford
  • 418
  • 6
  • 14