我正在开发一个将json文件下载到csv格式的解决方案。
在Chrome中,我可以毫无问题地下载文件。但是当我在IE中执行相同的任务时,它会刷新窗口并打印并返回到原始窗口。
这是我的代码:
var jsonData = [{
"id": "1",
"fname": "Ray",
"lname" : "Mak",
"Company" : "Microsoft"
}, {
"id": "2",
"fname": "Sam",
"lname" : "Smith",
"Company" : "Google"
}];
function msieversion() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer, return true
{
return true;
} else { // If another browser,
return false;
}
return false;
}
function JSONToCSVConvertor(JSONData,fileName,ShowLabel) {
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]) {
var arrValue = arrData[i][index] == null ? "" : '="' + arrData[i][index] + '"';
row += arrValue + ',';
}
row.slice(0, row.length - 1);
CSV += row + '\r\n';
}
if (CSV == '') {
growl.error("Invalid data");
return;
}
var fileName = "Result";
if(msieversion()){
var IEwindow = window.open();
IEwindow.document.write('sep=,\r\n' + CSV);
IEwindow.document.close();
IEwindow.document.execCommand('SaveAs', true, fileName + ".csv");
IEwindow.close();
} else {
var uri = 'data:application/csv;charset=utf-8,' + escape(CSV);
var link = document.createElement("a");
link.href = uri;
link.style = "visibility:hidden";
link.download = fileName + ".csv";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
JSONToCSVConvertor(jsonData,"Results", true);
这是我的JSFiddle link。