相关疑难解决方法(0)

使用express js在浏览器中显示Pdf

app.post('/asset', function(request, response){
  var tempFile="/home/applmgr/Desktop/123456.pdf";
  fs.readFile(tempFile, function (err,data){
     response.contentType("application/pdf");
     response.send(data);
  });
});
Run Code Online (Sandbox Code Playgroud)

我是表达js的新bie,我不能用数据对象发送响应.二进制内容在浏览器中可见.给我建议如何处理这个?

javascript pdf buffer node.js express

27
推荐指数
5
解决办法
5万
查看次数

NodeJs/Express 在浏览器中显示pdf文件

我正在开发 NodeJs/Express 项目,需要在浏览器中显示 pdf 文件,该文件存储在

/公共/图像

这是相关的路由器代码:

router.post('/show_file', async (req,res)=>{
  try {
     let path = './public/images/1.pdf'
     var data =fs.readFileSync(path);
     res.contentType("application/pdf");
     res.send(data);
   } catch (err) {
     res.status(500)
     console.log(err)
     res.send(err.message)
   }
})
Run Code Online (Sandbox Code Playgroud)

我没有收到任何错误,但没有发生任何事情,即浏览器未打开等。提前感谢您的任何指导。

browser pdf readfile node.js express

2
推荐指数
1
解决办法
6344
查看次数

如何使用 API 路由在 Next.js 上下载文件

我正在使用 next.js。我有一个第 3 方服务,我需要从中检索 PDF 文件。该服务需要一个我不想在客户端公开的 API 密钥。

这是我的文件

/api/getPDFFile.js ...

  const options = {
    method: 'GET',
    encoding: 'binary',
    headers: {
      'Subscription-Key': process.env.GUIDE_STAR_CHARITY_CHECK_API_PDF_KEY,
      'Content-Type': 'application/json',
    },
    rejectUnauthorized: false,
  };

  const binaryStream = await fetch(
    'https://apidata.guidestar.org/charitycheckpdf/v1/pdf/26-4775012',
    options
  );
  
  return res.status(200).send({body: { data: binaryStream}}); 


Run Code Online (Sandbox Code Playgroud)

页面/getPDF.js

   <button type="button" onClick={() => {
  fetch('http://localhost:3000/api/guidestar/charitycheckpdf',
    {
      method: 'GET',
      encoding: 'binary',
      responseType: 'blob',
    }).then(response => {
      if (response.status !== 200) {
        throw new Error('Sorry, I could not find that file.');
      }
      return response.blob();
    }).then(blob => {
      const …
Run Code Online (Sandbox Code Playgroud)

download next.js

2
推荐指数
1
解决办法
810
查看次数

标签 统计

express ×2

node.js ×2

pdf ×2

browser ×1

buffer ×1

download ×1

javascript ×1

next.js ×1

readfile ×1