Polymer Iron Ajax PDF响应 - 阻止Chrome弹出窗口被阻止

时间:2018-03-30 09:17:02

标签: javascript ajax pdf download polymer

我正在使用Polymer Iron Ajax从服务器获取PDF文档。但是,当在客户端上强制下载时,Chrome(版本65.0.3325.181)会显示消息“弹出窗口被阻止” - 从可用性的角度来看这是不可接受的。任何想法如何解决这个问题,以免下载?我已经简化了下面的代码,因为PDF是使用Puppeteer从ajax请求中包含的数据动态生成的。

使用Express.js的服务器代码:

response.type('application/pdf');
response.attachment('resume.pdf');
response.send(mypdf);

app.listen(8080)

Polymer App code:

<iron-ajax id="myironajax" method="GET" handle-as="blob" on-response="handlePdfResponse" url="http://localhost:8080/pdf"></iron-ajax>

handlePdfResponse(e) {
    var file = new Blob([e.detail.response], { type: 'application/pdf' });
    var fileURL = URL.createObjectURL(file);
    var a = document.createElement('a');
    a.href = fileURL;
    a.target = '_blank';
    a.download = 'myfile.pdf';
    document.body.appendChild(a);
    a.click();
  }

任何帮助非常感谢。感谢。

0 个答案:

没有答案
相关问题