gpt4 book ai didi

javascript - URL.createObjectURL(blob) 未在 IE 11 中创建正确的 URL

转载 作者:行者123 更新时间:2023-11-28 04:24:55 24 4
gpt4 key购买 nike

我正在尝试在 iframe 内渲染 pdf。它在 Mozilla (v54) 和 Chrome (v59) 上运行良好,但当我单击加载 PDF 的链接时,在 IE(v11) 中没有任何反应。经过多次调试发现Chrome/Firefox中的URL是blob:http://localhost:37444/5a8e7fed-cd61-4c58-904c-fad2ae169718在 IE(v11) 中,它是 blob:B7395CB5-169D-471F-BB8F-AA90EAFB6DDB。为什么 URL.createObjectURL(blob) 没有在 IE(v11) 中附加 http 请求

function (iframe, url, headers) {            

var xhr = new XMLHttpRequest();

xhr.open('GET', url);
xhr.onreadystatechange = handler;
xhr.responseType = "arraybuffer";

headers.forEach(function (header) {
xhr.setRequestHeader(header[0], header[1]);
});
xhr.send();

function handler() {
if (this.readyState === this.DONE) {

if (this.status === 200) {
var blob = new Blob([xhr.response], { type: "application/pdf" });
var objectUrl = URL.createObjectURL(blob);

iframe.src = objectUrl;
} else {
console.error('XHR failed', this);
}
}
}

最佳答案

我认为,出于安全原因,IE 不会为这些 blob 对象创建 url。因此,使用 var objectUrl = URL.createObjectURL(blob); 不会为您提供可以在 iframe 或嵌入标记内使用的源 url。我遇到了同样的问题,并搜索了很多有关修复的信息。但无法得到答案。相反,我按如下方式解决了它。您可以在 IE 中使用以下内容

if (bowser.msie && window.navigator.msSaveOrOpenBlob) {
navigator.msSaveOrOpenBlob(file, fileName);
}else{
//do what you were doing for other than IE
}

上面的IE代码会提示用户是要保存文件还是直接打开文件。用户可以单击“打开”按钮,然后 IE 将显示 PDF,而无需在默认阅读器中下载它。

关于javascript - URL.createObjectURL(blob) 未在 IE 11 中创建正确的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45142722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com