0

In my backend API, i am streaming a pdf in application/octet-stream format.

In front end, for any browser i just create a blob, and open it after:

var request = new XMLHttpRequest();
request.open("GET", url, true);
request.setRequestHeader("Authorization", "Bearer " + token);
request.responseType = "arraybuffer";
request.send();

request.onload = function () {
    var blob = new Blob([this.response], { type: blobType }),newUrl = URL.createObjectURL(blob);
    window.open(newUrl, "_blank", "menubar=yes,resizable=yes,scrollbars=yes");

And this works.

Internet explorer though, doesn't support blob, and i have to open it with:

window.navigator.msSaveOrOpenBlob(blob);

This will prompt the usual save or open file in IE. But i would like to open it straight in a new window.

I tried to use:

...
request.responseType = "application/pdf";
...
window.open("data:application/pdf," + this.response, "_blank", "menubar=yes,resizable=yes,scrollbars=yes");

and i tried as well

window.open("data:application/pdf;base64," + this.response, "_blank", "menubar=yes,resizable=yes,scrollbars=yes");

But it doesn't work.

Any of you knows how to open the pdf? what response type do i need to use? and then how to open it with window.open? Thanks

  • IE don't let you to open `blob` directly, you should use `msSaveOrOpenBlob` or `msSaveBlob` . – Aria Jan 09 '19 at 10:45
  • Yes you can cast `Blob` to other file, but it is not so easy, take a look at https://stackoverflow.com/questions/27159179/how-to-convert-blob-to-file-in-javascript convert blob to `png` file. – Aria Jan 09 '19 at 10:52
  • What's your IE browser version? If you are using IE 11, you could try to use [pdf.js](https://github.com/mozilla/pdf.js) to display the pdf. here are some similar threads, you could refer to them: [link1](https://stackoverflow.com/questions/34440114/need-to-open-blob-pdf-in-ie-window) and [link2](https://stackoverflow.com/questions/24007073/open-links-made-by-createobjecturl-in-ie11) – Zhi Lv Jan 10 '19 at 07:44
  • The last one could've worked, I've fixed it for you, but I'm not going to post it as an answer, since I have no idea if it would work on IE (it does work in Chrome, just not in a popup for security reasons, but I've managed to load the pdf in an iframe): ` window.open("data:application/pdf;base64," + window.btoa(String.fromCharCode.apply(String, new Uint8Array(this.response))), "_blank", "menubar=yes,resizable=yes,scrollbars=yes"); ` – wrock Jun 30 '21 at 08:08

0 Answers0