在IE11中打开由createObjectURL创建的链接 [英] Open links made by createObjectURL in IE11

查看:364
本文介绍了在IE11中打开由createObjectURL创建的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

http ://html5-demos.appspot.com/static/a.download.html



你甚至无法右键单击并在新标签页/窗口。是否有需要自定义的浏览器设置?

解决方案

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

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



以下是我的跨浏览器解决方案,适用于IE,Chrome和Firefox:

 

 < script src =https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/ jquery.min.js>< / script>< a id =exportclass =myButtondownload =href =#> export< / a>  


Why can't you open the link in the following demo:
http://html5-demos.appspot.com/static/a.download.html

You cannot even right click and open it in a new tab/window. Is there any setting in the browser I need to customize?

解决方案

This demo uses Blob URL which is not supported by IE due to security restrictions.

IE has its own API for creating and downloading files, which is called msSaveOrOpenBlob.

Here is my cross-browser solution that works on IE, Chrome and Firefox:

function createDownloadLink(anchorSelector, str, fileName){
    if(window.navigator.msSaveOrOpenBlob) {
        var fileData = [str];
        blobObject = new Blob(fileData);
        $(anchorSelector).click(function(){
            window.navigator.msSaveOrOpenBlob(blobObject, fileName);
        });
    } else {
        var url = "data:text/plain;charset=utf-8," + encodeURIComponent(str);
        $(anchorSelector).attr("download", fileName);
        $(anchorSelector).attr("href", url);
    }
}

$(function () {
    var str = "hi,file";
    createDownloadLink("#export", str, "file.txt");
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="export" class="myButton" download="" href="#">export</a>

这篇关于在IE11中打开由createObjectURL创建的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆