Internet Explorer fails opening a pdf string file

后端 未结 2 1757
暗喜
暗喜 2020-12-31 17:39

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

2条回答
  •  孤城傲影
    2020-12-31 18:40

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

    The link that I saw for my solution : https://viethoblog.wordpress.com/2016/08/30/loaddisplay-pdf-from-base64-string-bonus-ie-workaround/

提交回复
热议问题