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

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

问题描述

我从不管理的Web服务接收到一个包含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天全站免登陆