Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a new window with PDF data in a blob object returned from XMLHttpRequest

Inside JavaScript I am making an XMLHttpRequest to a server which generates a PDF and returns the PDF data. In Chrome I'm able to open this data in a new window/tab like this:

window.open( URL.createObjectURL(RETURNED_DATA) );

where RETURNED_DATA is the actual data returned from the XHR.

In IE10 (and I'm assuming older versions of IE), I'm getting "Permission denied" when attempting to window.open() with the DOMString object returned from URL.createObjectURL()

I've tried various versions of this such as opening the new window before sending the XHR and updating the new window's location in the XHR callback, and opening the new window with a simple HTML file that just contains the XHR so that it can update it's own location in the XHR callback. All of these variations work in Chrome but result in "Permission denied" errors in IE10.

My best guess is that it's caused by the same-origin policy. When logging the DOMString object to the console in Chrome vs IE, I've noticed that Chrome prepends the string with protocol and host so blob:http://localhost:8080/BLOB_DATA whereas IE10 just has blob:BLOB_DATA.

Any ideas how to resolve this issue in IE?

like image 854
bkbooth Avatar asked Sep 20 '13 10:09

bkbooth


People also ask

How do I open a blob file in PDF?

Open and Display PDFs from a Blob Using JavaScript. const documentBlobObjectUrl = URL. createObjectURL(blob); PSPDFKit. load({ document: documentBlobObjectUrl }) .

What is blob in PDF?

Convert PDF to Blob A Blob (or Binary Large Object) is an array of binary data.

What is the XMLHTTPRequest object?

XMLHttpRequest (XHR) objects are used to interact with servers. You can retrieve data from a URL without having to do a full page refresh. This enables a Web page to update just part of a page without disrupting what the user is doing. XMLHttpRequest is used heavily in AJAX programming.

How do I open a blob file in browser?

If you cannot open your BLOB file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a BLOB file directly in the browser: Just drag the file onto this browser window and drop it.


1 Answers

For IE 10 and 11 you can use msSaveOrOpenBlob

window.navigator.msSaveOrOpenBlob(blob, fileName);
like image 86
ColinM Avatar answered Oct 12 '22 12:10

ColinM