Presented below is the service I offer:
angular.module('LBTable').service('exportTable', function () {
function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel, fileName) {
//If JSONData isn't an object, parse the JSON string into one
var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
var CSV = '';
if (ShowLabel) {
var row = "";
for (var index in arrData[0]) {
row += index + ',';
}
row = row.slice(0, -1);
CSV += row + '\r\n';
}
for (var i = 0; i < arrData.length; i++) {
var row = "";
for (var index in arrData[i]) {
row += '"' + arrData[i][index] + '",';
}
row.slice(0, row.length - 1);
CSV += row + '\r\n';
}
if (CSV == '') {
alert("Invalid data");
return;
}
document.body.removeChild(link);
}
return {
exportCSV: JSONToCSVConvertor
}
});
This service basically takes a JSON
object and transforms it into a downloadable csv file.
The functionality works smoothly on Chrome and Firefox. However, in Internet Explorer (even the latest version), the file does not download and no errors appear in the console.
I am curious as to why this issue occurs. :(
(view simplified demonstration here)