1

I am new to angularJS.I want to download JSON file.It is working in chrome but not in Edge and Safari.In safari file is opening in browser only. Thanks for reading. Plz reply.

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');
                    }
                }
AAD
  • 73
  • 7
  • A comment to the answer [here](http://stackoverflow.com/a/30800715/1497533) says that this method does not work in internet explorer if the data is longer than 2000 characters. I would assume that you can't get around this, so you either have to shorten your data, or serve it from a server where you can set the headers. – ippi Apr 08 '17 at 08:41
  • @ippi-is it possible in safari? – AAD Apr 08 '17 at 08:50
  • If I knew the answer, I would make it an answer. For now it's mostly guesswork, but http://stackoverflow.com/q/417142/1497533 might be interesting, and https://github.com/eligrey/FileSaver.js might even help. – ippi Apr 08 '17 at 09:46
  • @ippi- ok,Thanks. – AAD Apr 08 '17 at 13:42
  • Did you ever figure this out? I’m having the same issue. – Brian Oct 11 '17 at 16:02
  • 1
    @Brian I used [link](http://danml.com/download.html).it is working for edge browser,chrome but for safari need to do save As manually. – AAD Oct 12 '17 at 16:48

0 Answers0