Google Chrome-在iFrame中无法下载PDF [英] Google Chrome - PDF Download in an iFrame not working

查看:518
本文介绍了Google Chrome-在iFrame中无法下载PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,由于最近的Chrome更新阻止使用javaScript直接打开数据URL:不允许将顶部框架导航到数据URL",因此我需要修改一些代码.我有一个ajax调用,该调用使用mPDF生成PDF,将其发送回base 64-并应在新选项卡中打开.这一直有效.但是由于这个问题,我现在想要做的是将PDF加载到新窗口中的iFrame中,以避免出现错误.那行得通,这就是我所拥有的:

So due to the recent Chrome update preventing a data URL from being opened directly using javaScript: "Not allowed to navigate top frame to data URL", I have some code that I need to modify. I have an ajax call that uses mPDF to generate a PDF, sends it back base 64 - and should open in a new tab. This always worked. However due to this issue, what I'm now trying to do is load that PDF into an iFrame in a new window to avoid the error. And that works, here's what I have:

$('.print-order-pdf').click(function(){

    var orderNumber = $(this).attr('order')
    var data = {
        pdfnonce: pdfAjax.pdfnonce,  
        action: 'getOrderPDF',
        orderNumber: orderNumber,
    }
    var win = window.open('', '_blank');
    $.post(pdfAjax.ajax_url, data, function(return_url){           
        win.document.write("<iframe id='pdf' src='"+ return_url +"'></iframe>");
        win.document.write("<style> html, body{ overflow:hidden; margin:0; } #pdf{ width:100%; height: 100%; }</style>");
    });

});

(这是wordPress Ajax调用).无论如何-一切正常,带有PDF的新页面将打开.问题是-在Chrome中,作为PDF控件一部分显示的下载链接不再起作用.我可以打印,旋转,无论如何-但不能下载文件.我已经在firefox中进行了测试,没有任何问题可以触发那里的下载. PDF窗口的Chrome控制台完全没有显示任何错误.有任何想法吗? Chrome浏览器中是否有某些特定功能阻止从iFrame内部进行下载?为什么?有办法解决吗?

(This is a wordPress Ajax call). In any case - it all works and the new page with PDF opens. The problem is - in Chrome, the download link that appears as part of the PDF controls no longer works. I can print, rotate, whatever - but not download the file. I've tested in firefox, no issue triggering the download there. The Chrome console for the PDF window shows no errors at all. Any ideas? Is there something specific in Chrome preventing the download from inside the iFrame? Why? Is there a way around it?

非常感谢.

推荐答案

我的情况就是这样! 我正在使用Chrome版本61.0.3163.100. 我想这是浏览器的缺陷,但我在Google论坛上没有找到任何东西. 我不确定是否直接下载文件,但不想放弃chrome PDF阅读器.

My scenario is exactly that! I'm using chrome version 61.0.3163.100. I guess it's some browser flaw, but I did not find anything on google's forums. I am in doubt whether to download the file directly but would not want to give up the chrome PDF viewer.

在找到明确答案之前,我暂时找到了一个丑陋且不雅致的解决方案.我在生成的窗口中创建了一个链接:

Temporarily, I have found an ugly and inelegant solution until I have a definitive answer. I created a link in the generated window:

var newWindow = window.open("", "PDF", 'dependent=yes,locationbar=no,scrollbars=no,menubar=no,resizable,screenX=50,screenY=50,width=850,height=800');
newWindow.document.write(
'<html><body><center>' +
'<a title="Download File" style="font-family: \'Verdana\';color: #333;text-decoration: none;font-weight: 600;" download="File.PDF" href="data:application/pdf;base64,' + base64 + '">Download File</a>' +
'</center><br>' +
'<object width=100% height=100% type="application/pdf" data="data:application/pdf;base64,' + base64  + '">' +
'<embed type="application/pdf" src="data:application/pdf;base64,' + base64  + '" id="embed_pdf"></embed>' +
 '</object></body></html>');
 newWindow.window.focus();

这篇关于Google Chrome-在iFrame中无法下载PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆