Skip to content

Instantly share code, notes, and snippets.

@snekse
Last active February 15, 2016 20:15
Show Gist options
  • Save snekse/8483503431b50b87db5c to your computer and use it in GitHub Desktop.
Save snekse/8483503431b50b87db5c to your computer and use it in GitHub Desktop.
Export data to CSV client side in IE without data URI link

Export data to CSV client side in IE without data URI link

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
};
body(ng-controller="MainCtrl")
iframe(id="csvDownloadFrame", style="display:none")
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> &nbsp;&nbsp;";
**/
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> &nbsp;&nbsp;";
csvDataLinkHtml += "</br></span>"; //End csv-data-link-span
fp.append(csvDataLinkHtml);
//....
}
@myGitHubForum
Copy link

Thanks for the Solution. It solved my days of work and trials..!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment