【问题标题】:PDF is blank when openedPDF打开时为空白
【发布时间】:2020-09-09 20:40:00
【问题描述】:

我已经在这里待了好几个小时了,但我什么也没有。我正在尝试从我的服务器下载 PDF 文件。我目前正在本地主机上运行它。当我打开 PDF 时,PDF 是一个白色的空白页。有人可以让我知道我错过了什么。我检查了保存在服务器上的PDF,没问题。

应用程序 - React、Express。

NPM - Axios,文件保护程序,html-pdf

路径 - Front end React

.then(() =>
  axios.get(
    'http://localhost:3000/retailers/fetch/pdf',
    {
      headers: {
        'Content-type': 'application/json',
        'x-auth-token': this.props.token
      }
    },
    { responseType: 'blob' }
  )
)
.then((res) => {
  console.log('dd', res);
  const pdfBlob = new Blob([res.data], { type: 'application/pdf' });

  saveAs(pdfBlob, 'newPdf.pdf');
});

路径 - Backend express

const options = {
  root: path.join(__dirname, '../'),
  dotfiles: 'deny',
  headers: {
    'x-timestamp': Date.now(),
    'x-sent': true
  }
};

var fileName = 'test.pdf';
res.sendFile(fileName, options, function (err) {
  if (err) {
    console.log('err', err);

    next(err);
  }
});

【问题讨论】:

    标签: javascript reactjs express blob filesaver.js


    【解决方案1】:

    所以答案很简单。 responseType: 'blob' 放错地方了。

    axios.get(
      'http://localhost:3000/retailers/fetch/pdf',
      {
        headers: {
          'Content-type': 'application/json',
          'x-auth-token': this.props.token
        },
        responseType: 'blob'
      }
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-09
      • 2014-10-06
      • 1970-01-01
      • 2018-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多