鉴于此代码(来自其他人):
var module = angular.module('myApp', []);
module.controller('MyCtrl', function ($scope){
$scope.json = JSON.stringify({a:1, b:2});
});
module.directive('myDownload', function ($compile) {
return {
restrict:'E',
scope:{ data: '=' },
link:function (scope, elm, attrs) {
function getUrl(){
return URL.createObjectURL(new Blob([JSON.stringify(scope.data)], {type: "application/json"}));
}
elm.append($compile(
'<a class="btn" download="backup.json"' +
'href="' + getUrl() + '">' +
'Download' +
'</a>'
)(scope));
scope.$watch(scope.data, function(){
elm.children()[0].href = getUrl();
});
}
};
});
The fiddle example在 chrome 中下载效果很好。但是单击“下载”链接在 IE11 中没有任何作用。没有错误,没有警告,根本没有响应。
但根据this它在 IE10 和 11 中受支持。
是否有一些 IE 安全设置需要更改或发生了什么?
最佳答案
为此找到了解决方案。首先,IE 以不同方式处理 blob 保存,特别是它使用:
window.navigator.msSaveOrOpenBlob(new Blob([element], {type:"text/plain"}), "filename.txt");`
如果您查看 this page 的源代码,您将看到他们是如何做到的。
但是要让它与跨浏览器支持一起工作是一件痛苦的事情……而且一天结束时你会得到 with FileSaver.js .
..这就是我最终使用的,而且效果很好。
关于javascript - 带有 angularjs 的 Internet Explorer 中的 Blob url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20533980/