[英]Chrome PDF viewer download button doesn't work on window.open() tab
I'm working on a project, which sends http requests to the spring boot.我正在做一个项目,它向 spring boot 发送 http 请求。 As response I receive a stream with PDF file.
作为回应,我收到了一个带有 PDF 文件的流。 I need to open this file in a new tab with all functionality of Chrome PDF viewer, especially downloading.
我需要使用 Chrome PDF 查看器的所有功能在新选项卡中打开此文件,尤其是下载。
Here's my code of processing response and opening a new tab with received PDF file:这是我处理响应并使用收到的 PDF 文件打开新选项卡的代码:
fetch(options.url, options)
.then(response => {
return response.blob();
})
.then(blob => {
const reader = new FileReader();
reader.readAsDataURL(blob);
reader.onload = () => {
const data = reader.result
const tab = window.open();
tab.document.write("<html>" +
"<body>" +
"<embed type='application/pdf' " +
"style='position:absolute; left: 0; top: 0;' " +
"width='100%' " +
"height='100%' " +
"src='" + data +"'/>" +
"</body>" +
"</html>")
}
})
But there is a problem after new tab opened: all buttons of Chrome PDF viewer work correctly except for 'download' button.但是在打开新标签页后出现了一个问题:Chrome PDF 查看器的所有按钮都可以正常工作,除了“下载”按钮。
Nothing happens after clicking the button.单击按钮后没有任何反应。
Do you have an idea to solve this problem?你有解决这个问题的想法吗?
Try replacing const tab = window.open();
尝试更换
const tab = window.open();
with const tab = window.open('about:blank', '_blank', "width=200,height=100");
with
const tab = window.open('about:blank', '_blank', "width=200,height=100");
I made a little change to Tafadzwa Chimberengwa's answer to open in full screen (edited):我对Tafadzwa Chimberengwa 的答案做了一点改动以全屏打开(已编辑):
window.open('about:blank', '_blank', 'width=100%, height=100%');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.