无法通过打印对话框在IE中打开Blob网址

时间:2019-09-27 03:51:08

标签: asp.net-mvc file pdf internet-explorer

我正在尝试在IE Edge中使用打印对话框打开PDF文件,它在chrome中可以正常工作,但在IE中不能正常工作

使用Evo Pdf工具返回文件的MVC代码:

var restClient = new RestClient(Request.Url.Scheme + "://" + Request.Url.Authority);
var restResponse = restClient.Execute(request);
if (restResponse.StatusCode == HttpStatusCode.OK)
 {
     htmlModel.HtmlString = restResponse.Content;
     byte[] pdfBytes = PdfUtil.GetEvoPdfBytes(htmlModel);
     if (pdfBytes != null)
        {
             return File(pdfBytes, System.Net.Mime.MediaTypeNames.Application.Pdf, htmlModel.PdfName + ".pdf");
        }
 }

使用打印对话框打开文件的Javascript代码,以下代码适用于chrome,但不适用于IE:

var req = new XMLHttpRequest();
        req.open("POST", "/api/HtmlToPdf", true);
        req.setRequestHeader("Content-Type", "application/json");
        req.responseType = "blob";

        req.onload = function (event) {
            var blob = req.response;
            console.log(blob.size);
            var lin = window.URL.createObjectURL(blob);
            // Works in chrome
            var mywindow = window.open(lin, "_blank");
            mywindow.focus();
            mywindow.print();
        };
        req.send(JSON.stringify(
            {
                htmlModel: {
                    ElementSelector: "#div",
                    PageOrientation: "Portrait",
                    PdfName: "abc"
                }
            }));

1 个答案:

答案 0 :(得分:0)

IE11不支持 URL.createObjectURL()。因此,您的代码将不适用于IE浏览器,并且您将无法通过打印对话框打开Blob。

要解决此问题,您需要将 msSaveBlob msSaveOrOpenBlob 用于Internet Explorer浏览器。

这些方法允许用户将文件保存在客户端上,就像文件已从Internet下载一样。

var blobObject = new Blob(["This is sample text..."]);

window.navigator.msSaveOrOpenBlob (blobObject, 'msSaveOrOpenBlob_testFile.txt');  

参考文献:

(1)Download a blob from HTTP URL in IE 11

(2)Saving files locally using Blob and msSaveBlob

(3)Blob download is not working in IE