首页 > 解决方案 > 为什么在客户端打开 pdf 时无法打开文档错误或空白 pdf?

问题描述

我在响应类似于http://www.orimi.com/pdf-test.pdf的 get api 中需要一些秘密信息,这些信息仅在用节点编写的中间件中可用。我想在客户端(浏览器)上打开 pdf。所以我点击了代理get api,它将点击中间件,中间件将点击后端服务器,但我无法打开文档和空白pdf。谁能告诉我这段代码有什么问题?

fetch(pdfApiMiddlewareUrl, {

                    method: "GET",

                    headers: {
                        "Content-Type": "application/pdf",
                        'Content-Disposition': 'inline; filename=your_file_name'
                    },
                    responseType : 'blob'
                    })
                    .then(res => res.blob())
                    .then(response => {
                            var blobUrl = URL.createObjectURL(response);
                            window.open(blobUrl);
                    })
                    .catch(error => {
                        console.log("HEREEEEEEEE");
                        console.log(error);
                    });

中间件代码:

var urlEndPointsToHit = decodeURIComponent(req.query.urlToHit);
var url = urlEndPointsToHit+'?secret='+secretInfo;
var options;
options = {
  url: url,
  method: 'GET',
  headers: {
     'Content-type': 'application/pdf'
   },
};
  if(options) {
    options.qs = req.query || {};
  }

request(options, function(err, resp, body) {
  req.locals = body;
  res.setHeader('Content-Type', 'application/pdf');
  res.setHeader('Cache-Control', 'no-cache');                  
  next();
});

标签: node.jsexpresspdfblobweb-frontend

解决方案


推荐阅读