简体   繁体   English

在XMLHttpRequest返回的blob对象中打开一个包含PDF数据的新窗口

[英]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. 在JavaScript内部我正在向服务器发出XMLHttpRequest,该服务器生成PDF并返回PDF数据。 In Chrome I'm able to open this data in a new window/tab like this: 在Chrome中,我可以在新窗口/标签中打开此数据,如下所示:

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

where RETURNED_DATA is the actual data returned from the XHR. 其中RETURNED_DATA是从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() 在IE10中(我假设IE的旧版本),在尝试使用从URL.createObjectURL()返回的DOMString对象的window.open()时,我得到“Permission denied”

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. 我已经尝试了各种版本,例如在发送XHR之前打开新窗口并在XHR回调中更新新窗口的位置,并打开一个包含XHR的简单HTML文件的新窗口,以便它可以更新它自己在XHR回调中的位置。 All of these variations work in Chrome but result in "Permission denied" errors in IE10. 所有这些变体都适用于Chrome,但导致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 . 在Chrome vs IE中将DOMString对象记录到控制台时,我注意到Chrome在字符串前面加上协议和主机所以blob:http://localhost:8080/BLOB_DATA而IE10只有blob:BLOB_DATA

Any ideas how to resolve this issue in IE? 任何想法如何在IE中解决这个问题?

对于IE 10和11,您可以使用msSaveOrOpenBlob

window.navigator.msSaveOrOpenBlob(blob, fileName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM