【问题标题】:Chrome PDF viewer download button doesn't work on window.open() tabChrome PDF 查看器下载按钮在 window.open() 选项卡上不起作用
【发布时间】:2020-01-23 17:19:55
【问题描述】:

我正在开发一个项目,该项目将 http 请求发送到 spring boot。作为响应,我收到一个带有 PDF 文件的流。我需要使用 Chrome PDF 查看器的所有功能在新选项卡中打开此文件,尤其是下载。

这是我处理响应并使用收到的 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>")
            }
        })

但打开新标签后出现问题:Chrome PDF 查看器的所有按钮都可以正常工作,但“下载”按钮除外。

点击按钮后没有任何反应。

你有解决这个问题的想法吗?

【问题讨论】:

    标签: javascript html pdf


    【解决方案1】:

    尝试将const tab = window.open(); 替换为const tab = window.open('about:blank', '_blank', "width=200,height=100");

    【讨论】:

      【解决方案2】:

      我对@9​​87654321@ answer 做了一点更改以全屏打开(已编辑):

      window.open('about:blank', '_blank', 'width=100%, height=100%');
      

      【讨论】:

        猜你喜欢
        • 2018-05-15
        • 2017-01-23
        • 2017-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-30
        • 2014-08-25
        • 2022-09-27
        相关资源
        最近更新 更多