在angularjs Internet Explorer中的Blob网址 [英] Blob url in internet explorer with angularjs

查看:313
本文介绍了在angularjs Internet Explorer中的Blob网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有鉴于此code(从别人):

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();
            });
        }
    };
});

工作正常的小提琴例子在Chrome中下载。但点击下载链接,什么也不做的IE11。没有错误,没有警告,都没有反应。

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.

但根据此内容它支持IE10和11。

But according to this it's supported in IE10 and 11.

是否有需要改变或某些IE安全设置是怎么回事?

Is there some IE security setting that needs to be changed or what is going on?

推荐答案

找到一个解决方案。首先,IE处理BLOB储蓄不同,特别是它使用:
window.navigator.msSaveOrOpenBlob(新斑点([元] {类型:text / plain的}),FILENAME.TXT);

Found a solution for this. First, IE handles blob saving differently, specifically it uses: window.navigator.msSaveOrOpenBlob(new Blob([element], {type:"text/plain"}), "filename.txt");

如果你看一下源$ C ​​$ C为此页面,你会看到他们是怎么做吧。

If you look at the source code for this page, you will see how they do it.

但得到这个跨浏览器的支持工作是一种​​痛苦..和一天中的最后你会最终与FileSaver.js

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.

这篇关于在angularjs Internet Explorer中的Blob网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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