如何在angularJS中下载文件。用于Safari浏览器和边缘浏览器

时间:2017-04-07 10:40:12

标签: javascript angularjs safari

我是angularJS的新手。我想下载JSON文件。它正在使用chrome但不在Edge和Safari中。在safari文件中仅在浏览器中打开。 谢谢阅读。 Plz回复。

function itemExport() {
                    var a = document.createElement("a");
                    document.body.appendChild(a);
                    //a.style = "display: none";
                    if ($scope.selectedRows.length > 0) {
                        commonFactory.itemExport($scope.selectedRows, $scope.className).then(function (data) {

                            var json = JSON.stringify(data.data),
                                blob = new Blob([json], {type: "octet/stream"});
                                    //url = window.URL.createObjectURL(blob);
                            var encodedUri = encodeURI("data:text/plain;charset=utf-8," + json);
                            a.setAttribute("href",encodedUri);
                            if ($scope.selectedRows.length === 1) {
                                commonFactory.getInstanceByUID($scope.domainUID, $scope.classUID, $scope.className, $scope.selectedRows[0], $scope.isCoreComponents).then(function (inst) {
                                    a.setAttribute("download", inst[0]._fields[0].properties.Name+".json");
                                    a.click();
                                    window.URL.revokeObjectURL(url);
                                    });
                            } else {
                                a.download = $scope.className+".json";
                                a.click();
                                window.URL.revokeObjectURL(url);
                            }
                        });
                    } else {
                        toastrFactory.warning('Please select at least one item to Export', 'Warning');
                    }
                }

0 个答案:

没有答案