Javascript 在IE11中打开createObjectURL创建的链接

Javascript 在IE11中打开createObjectURL创建的链接,javascript,html,internet-explorer,blob,Javascript,Html,Internet Explorer,Blob,为什么不能在下面的演示中打开链接: 您甚至不能右键单击并在新选项卡/窗口中打开它。浏览器中是否有需要自定义的设置?由于安全限制,此演示使用IE不支持的Blob URL IE有自己的用于创建和下载文件的API,称为msSaveOrOpenBlob 以下是我在IE、Chrome和Firefox上使用的跨浏览器解决方案: 函数createDownloadLink(主播选择器、str、文件名){ if(window.navigator.msSaveOrOpenBlob){ var fileData=

为什么不能在下面的演示中打开链接:


您甚至不能右键单击并在新选项卡/窗口中打开它。浏览器中是否有需要自定义的设置?

由于安全限制,此演示使用IE不支持的Blob URL

IE有自己的用于创建和下载文件的API,称为
msSaveOrOpenBlob

以下是我在IE、Chrome和Firefox上使用的跨浏览器解决方案:

函数createDownloadLink(主播选择器、str、文件名){
if(window.navigator.msSaveOrOpenBlob){
var fileData=[str];
blobObject=新的Blob(fileData);
$(主播选择器)。单击(函数(){
window.navigator.msSaveOrOpenBlob(blobobObject,文件名);
});
}否则{
var url=“data:text/plain;charset=utf-8,”+encodeURIComponent(str);
$(主播选择者).attr(“下载”,文件名);
$(主播选择者).attr(“href”,url);
}
}
$(函数(){
var str=“你好,文件”;
createDownloadLink(“导出”,str,“file.txt”);
});

如果数据来自Ajax,则可以添加

if (window.navigator.msSaveOrOpenBlob)
 xhttp.responseType = "arraybuffer";
else
 xhttpGetPack.responseType = "blob";
/////////////////////////////////////////////////

var a = document.createElement("a");
document.body.appendChild(a);
a.style.display = "none";

// IE
if (window.navigator.msSaveOrOpenBlob)
{
  a.onclick = ((evx) => 
  {
      var newBlob = new Blob([new Uint8Array(xhttpGetPack.response)]);
      window.navigator.msSaveOrOpenBlob(newBlob, "myfile.ts");
  });
  a.click();
}
else //Chrome and safari
{
  var file = URL.createObjectURL(xhttpGetPack.response);
  a.href = file;
  a["download"] = "myFile.ts";
  a.click();
  window.URL.revokeObjectURL(file);
}

这里是下载任何文件作为blob的函数。(在IE和非IE上测试)

注意:如果需要,请更改文件的类型。

//数据变量中ajax成功返回文件对象
        //File Object return in ajax Success in data variable
         var blob = new Blob([data]);
         if (navigator.appVersion.toString().indexOf('.NET') > 0) //For IE
          {
            window.navigator.msSaveOrOpenBlob(blob, "filename.ext");
          }
          else if (navigator.userAgent.toLowerCase().indexOf('firefox') >-1) 
              {
                var link = document.createElement('a');
                link.href = window.URL.createObjectURL(blob);
                link.download = "filename.ext";
                document.body.appendChild(link);//For FireFox <a> tag event 
                //not working
                link.click();
            }
          else
          {
                var link = document.createElement('a');
                link.href = window.URL.createObjectURL(blob);
                link.download = "filename.ext" 
                link.click();
          }
var blob=新blob([数据]); if(navigator.appVersion.toString().indexOf('.NET')>0)//用于IE { window.navigator.msSaveOrOpenBlob(blob,“filename.ext”); } else if(navigator.userAgent.toLowerCase().indexOf('firefox')>-1) { var link=document.createElement('a'); link.href=window.URL.createObjectURL(blob); link.download=“filename.ext”; document.body.appendChild(link);//用于FireFox标记事件 //不起作用 link.click(); } 其他的 { var link=document.createElement('a'); link.href=window.URL.createObjectURL(blob); link.download=“filename.ext” link.click(); }
要在Internet Explorer 11中下载内部iframe,您需要使用
parent.window.navigator.msSaveOrOpenBlob(blob,“filename.ext”)

可能不支持a标记上使用的下载属性。IE和Safari中不支持下载属性。但我不打算下载/保存链接:正如我在问题标题中提到的,它甚至没有打开/导航到链接。Safari工作正常。这是一个没有下载属性的。你看了吗?看起来是同样的问题。在一个网络工作者身上似乎不起作用:我真的希望“如果愚蠢的IE浏览器那么”的时代结束了。感谢您的提交,它帮助了我很多。JS Fiddle最近更改了其网站,以在iframe中显示结果。IE部分在iframe中不起作用。如果您在常规页面中尝试此代码,它也可以在IE 11中使用。@robert.bo.roth您想在客户端生成PDF,还是服务器上已经有PDF?如果它已经在服务器上,我建议使用PDF.JS()。对于客户端pdf生成(动态),请使用jsPDF()@robert.bo.roth不幸的是,IE不支持内嵌pdf。唯一的方法是向用户显示下载链接。PDFObject()可以检测浏览器支持并在内联PDF和下载链接之间切换。实际上不需要将元素附加到正文中,因此设置
样式
和调用
删除()
也是不必要的。在铬、FF和边缘铬上进行了测试。
        //File Object return in ajax Success in data variable
         var blob = new Blob([data]);
         if (navigator.appVersion.toString().indexOf('.NET') > 0) //For IE
          {
            window.navigator.msSaveOrOpenBlob(blob, "filename.ext");
          }
          else if (navigator.userAgent.toLowerCase().indexOf('firefox') >-1) 
              {
                var link = document.createElement('a');
                link.href = window.URL.createObjectURL(blob);
                link.download = "filename.ext";
                document.body.appendChild(link);//For FireFox <a> tag event 
                //not working
                link.click();
            }
          else
          {
                var link = document.createElement('a');
                link.href = window.URL.createObjectURL(blob);
                link.download = "filename.ext" 
                link.click();
          }