使用Internet Explorer 10及更低版本保存Base64编码的PDF [英] Saving Base64 encoded PDF with Internet Explorer 10 and below

查看:158
本文介绍了使用Internet Explorer 10及更低版本保存Base64编码的PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用 Bootstrap Table 和<一个href =http://wenzhixin.net.cn/p/bootstrap-table/docs/extensions.html#export =nofollow>导出插件。我遇到的一个问题是导出插件不能用于IE,因为文件是使用数据URI输出的,IE不支持(我想我读过IE 11支持它,但我的组织中没有人使用IE 11,当然。)。

I have recently started using Bootstrap Table and the export plugin for it. A problem I came across is that the export plugin doesn't work for IE, because the files are output using Data URI, which IE doesn't support (I think I read that IE 11 supports it, but no one in my organization uses IE 11, of course.).

我感兴趣的唯一文件类型是Microsoft办公类型(Word,Excel)和PDF。
我为MS文件创建了一个有效的解决方法。

The only file types I am interested in exporting are Microsoft office types (Word, Excel) and PDFs. I have created a functioning workaround for the MS files.

  if (supportsDataUriNavigation())
    window.open('data:application/vnd.ms-' + defaults.type + ';filename=exportData.doc;' + base64data);
  else {
    var iframe = document.getElementById("dataFrame");
    iframe = iframe.contentWindow || iframe.contentDocument;
    iframe.document.open("text/html", "replace");
    iframe.document.write(excelFile);
    iframe.document.close();
    iframe.focus();

    iframe.document.execCommand("SaveAs", true, defaults.fileName + fileExtension);
  }

在此检查中,如果浏览器是IE,那么我使用代码I找到此处。这适用于MS文件类型,但是当我尝试对PDF文件执行相同操作时,它会尝试另存为.htm文件。此文件为空。如果我在代码中用.pdf替换fileExtension变量,单击导出时将不会显示SaveAs窗口。没有javascript错误。

In this check, if the browser is IE, then I use the code I found here. This works great for the MS file types, but when I try to do the same for the PDF file, it tries to save as a .htm file. This file is blank. If I replace the fileExtension variable with .pdf in my code, the SaveAs window will not display when I click to export. There are no javascript errors.

我将embed标签的src属性设置为插件javascript返回的字符串,它看起来像这样:'application / pdf; base64,'+ Base64.encode(buffer)

I set the src attribute of an embed tag to a string that is returned by the plugin javascript, and it looks like so: 'application/pdf;base64,' + Base64.encode(buffer)

任何人都知道任何解决方案/解决方法吗?

Anyone know of any solution/workaround?

推荐答案

我知道这是一个非常古老的问题,但我找到了解决方案,我想分享有同样问题的人。你可以在这里看到演示:
https://jsfiddle.net/quangminh_ln/hy36tnt6/

I know this is very old question, but 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 10及更低版本保存Base64编码的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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