在 IE11 中打开 createObjectURL 创建的链接 [英] Open links made by createObjectURL in IE11
问题描述
为什么以下demo中的链接打不开:
http://html5-demos.appspot.com/static/a.download.html
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?
推荐答案
由于安全限制,此演示使用了 IE 不支持的 Blob URL.
This demo uses Blob URL which is not supported by IE due to security restrictions.
IE 有自己的用于创建和下载文件的 API,称为 msSaveOrOpenBlob
.
IE has its own API for creating and downloading files, which is called msSaveOrOpenBlob
.
这是我的跨浏览器解决方案,适用于 IE、Chrome 和 Firefox:
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屋!