Search code examples
javascriptajaxinternet-explorer

Download and open PDF in IE v11


I am sending byte array from backend and trying to open it with ajax and JS, I am always having corrupted PDf which cannot be opened. me code is below.

$.ajax({
responseType: 'application\pdf',
sucess: function (response)
{
var blob=new blob([response]),{type:'application\pdf'};
window.navigator.msSaveOrOpen(blob);
}
});

any help would be much appreciated. thank you


Solution

  • First, set a break point in the success function, then try to use F12 developer tools to debug your code and make sure you could get the pdf blob. Then, use the window.navigator.msSaveOrOpenBlob() method to download pdf file.

    Code as below:

    var req = new XMLHttpRequest();
    req.open("GET", "/44678.pdf", true);
    req.responseType = "blob";
    req.onload = function (event) {
        var blob = req.response;
        var newBlob = new Blob([blob], { type: "application/pdf" })
    
        // IE doesn't allow using a blob object directly as link href
        // instead it is necessary to use msSaveOrOpenBlob
        if (window.navigator && window.navigator.msSaveOrOpenBlob) {
            window.navigator.msSaveOrOpenBlob(newBlob);
            return;
        }
    };
    

    More details, you could check this article.

    Edit:please check your code, the Ajax method doesn't have the request url and have a spelling mistake at the success function.