Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jquery将JSON导出到CSV IE下载问题_Javascript_Jquery_Json_Internet Explorer - Fatal编程技术网

Javascript Jquery将JSON导出到CSV IE下载问题

Javascript Jquery将JSON导出到CSV IE下载问题,javascript,jquery,json,internet-explorer,Javascript,Jquery,Json,Internet Explorer,我正在开发一个将json文件下载为csv格式的解决方案 在Chrome中,我可以毫无问题地下载文件。但当我在IE中执行相同的任务时,它会刷新窗口并打印并返回到其原始窗口 这是我的密码: var jsonData = [{ "id": "1", "fname": "Ray", "lname" : "Mak", "Company" : "Microsoft" }, { "id": "2", "fname": "Sam", "lname" : "

我正在开发一个将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);
var jsonData=[{
“id”:“1”,
“fname”:“Ray”,
“lname”:“Mak”,
“公司”:“微软”
}, {
“id”:“2”,
“fname”:“Sam”,
“lname”:“Smith”,
“公司”:“谷歌”
}];
函数msieversion(){
var ua=window.navigator.userAgent;
变量msie=ua.indexOf(“msie”);
if(msie>0 | |!!navigator.userAgent.match(/Trident.*rv \:11\./)//如果是Internet Explorer,则返回true
{
返回true;
}else{//如果另一个浏览器,
返回false;
}
返回false;
}
函数JSONToCSVConvertor(JSONData、文件名、ShowLabel){
var arrData=typeof JSONData!=“object”?JSON.parse(JSONData):JSONData;
var CSV=“”;
如果(显示标签){
var行=”;
对于(arrData[0]中的var索引){
行+=索引+',';
}
row=row.slice(0,-1);
CSV+=行+'\r\n';
}
对于(变量i=0;i

这是我的JSFIDLE。

尝试使用上一篇文章中建议的解决方案