javascript - 带有 angularjs 的 Internet Explorer 中的 Blob url

标签 javascript html internet-explorer angularjs bloburls

鉴于此代码(来自其他人):

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/

相关文章:

javascript - 使用 Express 和 MongoDB - 如何注销用户?

html - CSS右边距在具有滚动溢出的Div内不起作用

javascript - jQuery 表格排序更改按列排序的颜色 :

javascript - ExtJS + IE10脚本执行顺序问题

javascript - 在 underscore.js 模板引擎中运行模板中的模板(递归)

php - 如何将表单数据存储在 MULTIPAGE 表单中?

javascript - 如何更改之前由 jQuery 创建的 div 的值

javascript - 垂直对齐未知宽度的文本

html - 哪些版本的 IE 支持 PNG 图标?

javascript - 如果浏览器不是 Internet Explorer 9 或更高版本,则显示一条消息