admin管理员组文章数量:1206591
Given this code (from someone else):
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 works fine to download in chrome. But clicking the 'download' link does nothing in IE11. No error, no warning, no response at all.
But according to this it's supported in IE10 and 11.
Is there some IE security setting that needs to be changed or what is going on?
Given this code (from someone else):
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 works fine to download in chrome. But clicking the 'download' link does nothing in IE11. No error, no warning, no response at all.
But according to this it's supported in IE10 and 11.
Is there some IE security setting that needs to be changed or what is going on?
Share Improve this question edited Dec 12, 2013 at 16:36 Nicros asked Dec 12, 2013 at 2:35 NicrosNicros 5,18312 gold badges60 silver badges102 bronze badges 2 |2 Answers
Reset to default 17Found a solution for this. First, IE handles blob saving differently, specifically it uses:
window.navigator.msSaveOrOpenBlob(new Blob([element], {type:"text/plain"}), "filename.txt");`
If you look at the source code for this page, you will see how they do it.
But to get this to work with cross browser support is a pain.. and a the end of the day you will end up with FileSaver.js.
..which is what I ended up using, and works like a charm.
Use FileSaver.js! So easy to use.
For PDF sent as binary response:
// In HTML, first include filesaver.js with <script src="/scripts/filesaver.js"></script>
var thisPDFfileName = 'my.pdf';
var fileData = new Blob([response], { type: 'application/pdf' });
// Cross-browser using FileSaver.js
saveAs(fileData, thisPDFfileName);
For NPM version:
var fileSaver = require('file-saver');
var thisPDFfileName = 'my.pdf';
var fileData = new Blob([response], { type: 'application/pdf' });
// Cross-browser using FileSaver.js
fileSaver.saveAs(fileData, thisPDFfileName);
Works like a charm, cross-browser.
Thanks to @Nicros for pointing filesaver.js out in his answer. I made this answer so users can quickly copy and paste an example of it, who don't want to use MS specific code. (Cross-browser rocks)
本文标签: javascriptBlob url in internet explorer with angularjsStack Overflow
版权声明:本文标题:javascript - Blob url in internet explorer with angularjs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738681922a2106603.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
text/plain
, to no avail. Interestingly, I can right-click, save target as, and that works. – bhamlin Commented Jan 7, 2014 at 19:34