我可以在IE中打开对象URL吗?

时间:2013-07-30 15:48:48

标签: javascript html5 internet-explorer

我使用Blob和URL.createObjectURL()来获取某些二进制PDF数据的“对象URL”(blob :)。

在FireFox和Chrome中,如果我添加了<A/>链接HTML元素,并以编程方式将href设置为对象网址,则该链接可以点击并导致显示PDF。

在IE10中,我得到对象URL OK。链接HTML元素显示确定并具有blob:target。但点击它不会导致显示PDF。相反,我得到一个空白窗口,IE图标永远旋转。

然而,即使在IE10中,右键单击save-target-as,如链接 保存包含正确数据的PDF文件。该文件可以在浏览器外成功打开。

任何人都知道为什么IE没有向我显示PDF文件?有什么建议我怎么能这样做?或者,任何说服它的建议只是提供保存链接,而不是尝试并且无法打开它。

以下是重现问题的页面和脚本: -

<a href="" target="_blank" id="theLink">Click this link</a>
<script type="text/javascript">
    var TEST_PDF_DATA = "JVBERi0xLjYNJeLjz9MNMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZyAvUGFnZXMgMiAwIFIgPj4KZW5kb2JqCjIgMCBvYmoKPDwgL1R5cGUgL1BhZ2VzIC9LaWRzIFsgNyAwIFIgXSAvUmVzb3VyY2VzIDQgMCBSIC9Db3VudCAxIC9NZWRpYUJveCAzIDAgUiA+PgplbmRvYmoKMyAwIG9iagpbIDAgMCA1OTUgODQyIF0KZW5kb2JqCjQgMCBvYmoKPDwgL0ZvbnQgNSAwIFIgPj4KZW5kb2JqCjUgMCBvYmoKPDwgL2RlZmF1bHQgNiAwIFIgPj4KZW5kb2JqCjYgMCBvYmoKPDwgL1R5cGUgL0ZvbnQgL0Jhc2VGb250IC9Db3VyaWVyLUJvbGQgL1N1YnR5cGUgL1R5cGUxID4+CmVuZG9iago3IDAgb2JqCjw8IC9QYXJlbnQgMiAwIFIgL1R5cGUgL1BhZ2UgL0NvbnRlbnRzIFsgOCAwIFIgXSAvUmVzb3VyY2VzIDQgMCBSIC9NZWRpYUJveCAzIDAgUiA+PgplbmRvYmoKOCAwIG9iago8PCAvRmlsdGVyIC9GbGF0ZURlY29kZSAvTGVuZ3RoIDY4ID4+DXN0cmVhbQ14nDNQSOd1CuHVT0lNSyzNKVEwNlIISeM1VDAAQkMQz9zcWM/CHCiay6vhkZqTk68Qnl+Uk6KoqRCSxesawgsADSoQLA1lbmRzdHJlYW0KZW5kb2JqCnhyZWYNMCA5DTAwMDAwMDAwMDAgNjU1MzUgZg0KMDAwMDAwMDAxNSAwMDAwMCBuDQowMDAwMDAwMDY0IDAwMDAwIG4NCjAwMDAwMDAxNTYgMDAwMDAgbg0KMDAwMDAwMDE4NyAwMDAwMCBuDQowMDAwMDAwMjIwIDAwMDAwIG4NCjAwMDAwMDAyNTYgMDAwMDAgbg0KMDAwMDAwMDMyOSAwMDAwMCBuDQowMDAwMDAwNDI5IDAwMDAwIG4NCnRyYWlsZXINPDwgL1NpemUgOSAvUm9vdCAxIDAgUiA+Pg1zdGFydHhyZWYNNTY4DSUlRU9G";
    var byteCharacters = atob(TEST_PDF_DATA);

    var charCodeFromCharacter = function(c) { return c.charCodeAt(0); }
    var byteArrays = [];
    for (var offset = 0; offset < byteCharacters.length; offset += 1000)
    {
        var slice = byteCharacters.slice(offset, offset + 1000);
        var byteNumbers = Array.prototype.map.call(slice, charCodeFromCharacter);
        byteArrays.push(new Uint8Array(byteNumbers));
    }

    var blob = new Blob(byteArrays, { type: "application/pdf" });
    var url = URL.createObjectURL(blob);

    document.getElementById("theLink").href = url;
    document.getElementById("theLink").innerHTML = url;
</script>

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您应该先以用户操作打开网址,然后填充pdf

选中此一项:

how to bypass pop-up blocker in angular 6?