【问题标题】:IE how to open blob object in new TabIE如何在新选项卡中打开blob对象
【发布时间】:2016-10-22 14:46:15
【问题描述】:

您好,有人知道如何在 IE 新选项卡中打开 blob 对象

var blob = base64toBlob(blobresponse);
    if (blob) {
        if (window.navigator &&
            window.navigator.msSaveOrOpenBlob) {
            var a = document.createElement("a");
            document.body.appendChild(a);
            a.href = window.URL.createObjectURL(blob);
            a.target = "_blank"
            a.download = "test.pdf";
            a.click();
        } else {
            var objectUrl = URL
                .createObjectURL(blob);
            window
                .open(objectUrl);
        }
    }

【问题讨论】:

    标签: javascript jquery html internet-explorer


    【解决方案1】:

    据我所知,IE 在新标签页中打开 blob URL 时存在问题。 但是你可以强制它下载。

    window.navigator.msSaveOrOpenBlob(blob , 'test.pdf');
    

    但是如果你想在IE中查看PDF,可以尝试使用pdf.js(link)

    【讨论】:

    • 但我想打开而不是下载
    • @gayathri 没办法)))
    • @gayathri 如果想在ie中查看pdf,可以尝试使用pdf.js => mozilla.github.io/pdf.js
    • 但我不想使用任何其他插件这无论如何都会变得复杂谢谢你到目前为止我尝试了很多方法和论坛没有答案甚至堆栈溢出也得到了一个回复所以它我猜不可能是因为 IE 的安全问题。但谢谢@Alex
    • 有没有办法限制打开空白新标签?认为不必要地打开一个空白标签是没用的
    【解决方案2】:

    你可以使用 iframe 来做到这一点

    var blob = base64toBlob(blobresponse);
    
    if (blob) {
        var tab = window.open();
        var objectUrl = URL.createObjectURL(blob);
        if (window.navigator &&
            window.navigator.msSaveOrOpenBlob) { // for IE
            var iframe= document.createElement("iframe");
            tab.document.body.appendChild(iframe);  
            iframe.src = objectUrl;
            iframe.setAttribute("style","width:100%;height:100%");                      
        } else { // for Chrome           
            tab.location.href = objectUrl;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-06
      • 2011-07-29
      • 1970-01-01
      • 2010-11-14
      • 2014-05-14
      • 2011-12-12
      • 1970-01-01
      • 2014-01-10
      相关资源
      最近更新 更多