【问题标题】:Express .sendFile only works in chromeExpress .sendFile 仅适用于 chrome
【发布时间】:2015-05-19 17:14:10
【问题描述】:

我想向客户发送 PDF 文件,我使用 Express 中的 .sendFile 发送给它。 我在 Chrome 中运行良好,使用下载链接 chrome 保存它,使用正常的 _blank 链接,chrome 在新的 chrome 选项卡中打开它。

但在 Mozilla、Safari 和 IE 中,浏览器会显示消息“文件已损坏”或 adobe reader。通过 chrome 加载 pdf 时,Adobe Reader 不会显示此消息。

我的代码:

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

有人知道为什么吗?

瓦尔哈

【问题讨论】:

    标签: node.js pdf express adobe-reader sendfile


    【解决方案1】:

    我遇到了同样的问题,但我改为 res.download 并且效果很好。这种行为在 Chrome 中并不那么漂亮,但它完成了工作。有兴趣看看其他人是否有过这种使用 PDF 的经验并有一个好的解决方案。

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

    【讨论】:

      猜你喜欢
      • 2021-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 2018-01-13
      相关资源
      最近更新 更多