Export data to CSV client side in IE without data URI link
Last active
February 15, 2016 20:15
-
-
Save snekse/8483503431b50b87db5c to your computer and use it in GitHub Desktop.
Export data to CSV client side in IE without data URI link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var getCsvFileForIE = function(target) { | |
var csvData = target.attributes["data-csv"].value; | |
if ( ! supportsDataUri() ) { | |
csvData = decodeURIComponent(csvData); | |
var iframe = document.getElementById('csvDownloadFrame'); | |
iframe = iframe.contentWindow || iframe.contentDocument; | |
csvData = 'sep=,\r\n' + csvData; | |
iframe.document.open("text/html", "replace"); | |
iframe.document.write(csvData); | |
iframe.document.close(); | |
iframe.focus(); | |
iframe.document.execCommand('SaveAs', true, 'data.csv'); | |
} else { | |
if (console && console.log) { | |
console.log('Trying to call getCsvFileForIE with non IE browser.'); | |
} | |
} | |
}; | |
var supportsDataUri = function() { | |
var isOldIE = navigator.appName === "Microsoft Internet Explorer"; | |
var isIE11 = !!navigator.userAgent.match(/Trident\/7\./); | |
return ! (isOldIE || isIE11); //Return true if not any IE | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body(ng-controller="MainCtrl") | |
iframe(id="csvDownloadFrame", style="display:none") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function showDs() { | |
var csvData = ''; | |
// Populated csvData | |
//.... | |
var fp = grid.$root.find(".ngFooterPanel"); | |
var csvDataLinkPrevious = grid.$root.find('.ngFooterPanel .csv-data-link-span'); | |
if (csvDataLinkPrevious != null) {csvDataLinkPrevious.remove() ; } | |
var csvDataLinkHtml = "<span class=\"csv-data-link-span\">"; | |
/** | |
csvDataLinkHtml += "<br><a href=\"data:text/csv;charset=UTF-8,"; | |
csvDataLinkHtml += encodeURIComponent(csvData); | |
csvDataLinkHtml += "\" download=\"Export.csv\">CSV Export ORIGINAL</a> "; | |
**/ | |
csvDataLinkHtml += " <a "; | |
if ( ! supportsDataUri() ) { | |
csvDataLinkHtml += " data-csv=\""; | |
csvDataLinkHtml += encodeURIComponent(csvData); | |
csvDataLinkHtml += "\" onclick='getCsvFileForIE(this);' >"; | |
} else { | |
csvDataLinkHtml += "href=\"data:text/csv;charset=UTF-8,"; | |
csvDataLinkHtml += encodeURIComponent(csvData); | |
csvDataLinkHtml += "\" download=\"Export.csv\">"; | |
} | |
csvDataLinkHtml += "CSV Export</a> "; | |
csvDataLinkHtml += "</br></span>"; //End csv-data-link-span | |
fp.append(csvDataLinkHtml); | |
//.... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the Solution. It solved my days of work and trials..!!