From 8b9ea86bac38c27d57bb145c5957d4e5c1d3f04c Mon Sep 17 00:00:00 2001 From: hemakumar <hemakumarm@teamf1networks.com> Date: Wed, 6 Apr 2016 11:56:52 +0530 Subject: [PATCH 1/3] Enhancement: Updated to work for IE9 and Safari File downloaded with .txt extension in IE9 with saveas window, So here we can change extension to .csv while downloading. Some times IE9 failing to download file with '.csv' extension, in that scenario downloading with '.txt' extension works fine. In Safari file downloaded with file name 'Unknown' with no extension, didn't find any possibility to downloaded file as given name. For others browsers same code is followed. Still need to check for fixing the known bugs. --- src/ng-csv/directives/ng-csv.js | 57 ++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/src/ng-csv/directives/ng-csv.js b/src/ng-csv/directives/ng-csv.js index bfb80f9..213ec7c 100644 --- a/src/ng-csv/directives/ng-csv.js +++ b/src/ng-csv/directives/ng-csv.js @@ -85,14 +85,55 @@ angular.module('ngCsv.directives'). link: function (scope, element, attrs) { function doClick() { var charset = scope.charset || "utf-8"; - var blob = new Blob([scope.csv], { - type: "text/csv;charset="+ charset + ";" - }); - - if (window.navigator.msSaveOrOpenBlob) { - navigator.msSaveBlob(blob, scope.getFilename()); - } else { - + var ua = navigator.userAgent.toLowerCase(); + var safari, blob ; + //To findout safari browser + if (ua.indexOf('safari') != -1) { + if (ua.indexOf('chrome') > -1) { + safari = false; //chrome + } else { + safari = true; // Safari + } + } + //IE browsers + if ( ua.match(/msie/gi) || navigator.appName.match(/Internet/gi) || navigator.msMaxTouchPoints !== void 0 ){ + //IE 10+ browsers + if( window.navigator.msSaveOrOpenBlob ) { + blob = new Blob([scope.csv], { + type: "text/csv;charset="+ charset + ";" + }); + navigator.msSaveBlob(blob, scope.getFilename()); + }else{ + //IE9 and below browsers + csvData = decodeURIComponent(scope.csv); + var iframe = angular.element('<iframe></iframe>'); + iframe[0].style.display = "none"; + element.append(iframe); + var doc = null; + if (iframe[0].contentDocument) + doc = iframe[0].contentDocument; + else if (iframe[0].contentWindow) + doc = iframe[0].contentWindow.document; + doc.open("data:application/csv;charset=utf-8", "replace"); + doc.write(csvData); + doc.close(); + iframe.focus(); + if(!doc.execCommand('SaveAs', true, scope.getFilename())){ + //doc.execCommand('SaveAs', true, scope.getFilename()) returns false that means file downloading is failed with '.csv' extension. In IE9 '.csv' extension file not downloading properly, So while file saving is failed with extension '.csv' the same file got success while downloading with '.txt' extension and also we will get saveas option to save the file here we can change extension and file name. + doc.execCommand('SaveAs', true, scope.getFilename()+'.txt'); + } + } + } + //For safari browser + else if(safari){ + window.open('data:attachment/csv;filename='+scope.getFilename()+';charset=utf-8,' + encodeURI(scope.csv), "csvWindow"); + window.close(); + } + //Other than safari and IE browsers + else { + blob = new Blob([scope.csv], { + type: "text/csv;charset="+ charset + ";" + }); var downloadContainer = angular.element('<div data-tap-disabled="true"><a></a></div>'); var downloadLink = angular.element(downloadContainer.children()[0]); downloadLink.attr('href', window.URL.createObjectURL(blob)); From 24d2d05b52a83c32980f8cd5c10f39e27fd8e14c Mon Sep 17 00:00:00 2001 From: hemakumar0416 <mailmehema91@gmail.com> Date: Thu, 16 Jun 2016 23:32:32 +0530 Subject: [PATCH 2/3] modified readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2354ae2..e67ea87 100644 --- a/README.md +++ b/README.md @@ -79,3 +79,4 @@ Supported Browsers [](https://bitdeli.com/free "Bitdeli Badge") +## Running testcases \ No newline at end of file From 8b487949e1cea86ad1e3b4a51c840f1ad976d98b Mon Sep 17 00:00:00 2001 From: hemakumar0416 <mailmehema91@gmail.com> Date: Thu, 16 Jun 2016 23:43:33 +0530 Subject: [PATCH 3/3] readme modify --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e67ea87..7e79892 100644 --- a/README.md +++ b/README.md @@ -79,4 +79,6 @@ Supported Browsers [](https://bitdeli.com/free "Bitdeli Badge") -## Running testcases \ No newline at end of file +## Running testcases + +karma start \ No newline at end of file