Khalil M
Khalil M

Reputation: 1858

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

Upvotes: 0

Views: 237

Answers (2)

Axel EsZu
Axel EsZu

Reputation: 41

Having the same issue with IE, ajax headers fixed the problem

$.ajax({
          url: this.href,
          cache: false,
          xhrFields: {
            responseType: 'blob'
          },
          success: function (data) {
            var newBlob = new Blob([data], { type: "application/pdf" })
            navigator.msSaveOrOpenBlob(newBlob, "something.pdf");
          },
          error: function (error) {
            console.log(error);
          }
        });

Upvotes: 1

Zhi Lv
Zhi Lv

Reputation: 21353

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.

Upvotes: 0

Related Questions