IE how to open blob object in new Tab

2019-07-30 17:00发布

问题:

Hi any one have idea about how to open an blob object in IE new Tab

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);
        }
    }

回答1:

you can do it using 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;
    }
}


回答2:

As far I know, IE has a problem with opening blob URL in new tab. But you can force it to download.

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

But if you want to view PDF in IE, you can try to use pdf.js (link)