Internet Explorer 无法打开 pdf 字符串文件 [英] Internet Explorer fails opening a pdf string file

查看:29
本文介绍了Internet Explorer 无法打开 pdf 字符串文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到(来自我不管理的网络服务)一个包含 pdf 文件内容的字符串.

I receive (from a webservice I don't manage) a string with the content of a pdf file.

在客户端,我使用这个函数:

On client's side, I use this function:

window.open('data:application/pdf;base64,'+encodeURI(TheStringWithThePdfContent));

window.open('data:application/pdf;base64,'+encodeURI(TheStringWithThePdfContent));

像往常一样,它适用于除 IE(在我的情况下为 11 个)之外的所有浏览器,它会显示一条带有以下消息的警报:您要允许此网站在您的计算机上打开应用程序吗?"

As usual, it works in every browser but IE (11 in my case), which shows an alert with the message: "Do you want to allow this website to open an app on your computer?"

如果我说不,就会打开一个空白页面.

If I say no, an empty white page is opened.

如果我说是,它会尝试打开一个数据"文件(我猜它是从 window.open 中的协议读取的),并且因为它没有找到任何应​​用程序来执行此操作,所以将我发送到微软应用商店,它只是建议我下载iMusic"

If I say yes, it tries to open a "data" file (as it reads from the protocol in window.open, I guess) and, as it doesn't find any application to do that, sends me to the Microsoft application store, which just suggests me to download "iMusic"

当然,完全没用.

我已经更改了所有我认为可能会有所帮助的 Internet 选项,但都不起作用.

I've changed all the Internet Options I've guessed could help, none works.

有什么建议吗?

提前致谢,

推荐答案

我找到了解决方案,我想分享任何有同样问题的人.你可以在这里看到演示:https://jsfiddle.net/quangminh_ln/hy36tnt6/

I found the solution and I want to share anyone who has the same problem. You can see the demo here : https://jsfiddle.net/quangminh_ln/hy36tnt6/

'use strict';

var data = "...Your PDF base64 string...";
var fileName = "your_file_name";
if (window.navigator && window.navigator.msSaveOrOpenBlob) { // IE workaround
    var byteCharacters = atob(data);
    var byteNumbers = new Array(byteCharacters.length);
    for (var i = 0; i < byteCharacters.length; i++) {
        byteNumbers[i] = byteCharacters.charCodeAt(i);
    }
    var byteArray = new Uint8Array(byteNumbers);
    var blob = new Blob([byteArray], {type: 'application/pdf'});
    window.navigator.msSaveOrOpenBlob(blob, fileName);
}
else { // much easier if not IE
    window.open("data:application/pdf;base64, " + data, '', "height=600,width=800");
}

我看到的解决方案链接:https://viethoblog.wordpress.com/2016/08/30/loaddisplay-pdf-from-base64-string-bonus-ie-workaround/

The link that I saw for my solution : https://viethoblog.wordpress.com/2016/08/30/loaddisplay-pdf-from-base64-string-bonus-ie-workaround/

这篇关于Internet Explorer 无法打开 pdf 字符串文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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