Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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 Json到CSV转换文件名无效_Javascript_Json_Export To Csv - Fatal编程技术网

Javascript Json到CSV转换文件名无效

Javascript Json到CSV转换文件名无效,javascript,json,export-to-csv,Javascript,Json,Export To Csv,我已经编写了javascript代码将json转换为csv文件进行下载。每个文件都被正确的文件名转换和下载,除了一个文件的文件名问题。在Chrome上,其他文件以命名约定下载,如abc_def_ghi.csv,但只有文件以“下载”文件名下载,没有csv扩展名。但是,当您使用记事本“打开”时,它会显示正确的csv数据。我不知道它有什么问题。Firefox上也存在同样的问题,只是它设置了一些不符合约定的文件名,还设置了扩展名,但每次文件名都会更改为相同的csv 以下是Firefox请求“保存”时的屏

我已经编写了javascript代码将json转换为csv文件进行下载。每个文件都被正确的文件名转换和下载,除了一个文件的文件名问题。在Chrome上,其他文件以命名约定下载,如abc_def_ghi.csv,但只有文件以“下载”文件名下载,没有csv扩展名。但是,当您使用记事本“打开”时,它会显示正确的csv数据。我不知道它有什么问题。Firefox上也存在同样的问题,只是它设置了一些不符合约定的文件名,还设置了扩展名,但每次文件名都会更改为相同的csv

以下是Firefox请求“保存”时的屏幕截图

这是两次下载的同一个csv文件的文件名

这是从chrome下载的文件截图

下面是我编写的将json转换为csv并下载的代码

function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) {
                //If JSONData is not an object then JSON.parse will parse the JSON string in an Object
                var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;

                var CSV = '';
                //Set Report title in first row or line

                //CSV += ReportTitle + '\r\n\n';

                //This condition will generate the Label/Header
                if (ShowLabel) {
                    var row = "";

                    //This loop will extract the label from 1st index of on array
                    for (var index in arrData[0]) {

                        //Now convert each value to string and comma-seprated
                        row += index + ',';
                    }

                    row = row.slice(0, -1);

                    //append Label row with line break
                    CSV += row + '\r\n';
                }

                //1st loop is to extract each row
                for (var i = 0; i < arrData.length; i++) {
                    var row = "";

                    //2nd loop will extract each column and convert it in string comma-seprated
                    for (var index in arrData[i]) {
                        row += '"' + arrData[i][index] + '",';
                    }

                    row.slice(0, row.length - 1);

                    //add a line break after each row
                    CSV += row + '\r\n';
                }

                if (CSV == '') {
                    alert("Invalid data");
                    return;
                }

                //Generate a file name
                var fileName = "";
                //this will remove the blank-spaces from the title and replace it with an underscore
                fileName += ReportTitle.replace(/ /g, "_");

                //Check for ie
                var ie = getInternetExplorerVersion();
                if (ie == -1) {
                    //Initialize file format you want csv or xls
                    var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);

                    // Now the little tricky part.
                    // you can use either>> window.open(uri);
                    // but this will not work in some browsers
                    // or you will not get the correct file extension    

                    //this trick will generate a temp <a /> tag
                    var link = document.createElement("a");
                    link.href = uri;

                    //set the visibility hidden so it will not effect on your web-layout
                    link.style.visibility = "hidden";
                    link.download = fileName + ".csv";

                    //this part will append the anchor tag and remove it after automatic click
                    document.body.appendChild(link);
                    link.click();
                    document.body.removeChild(link);
                } else {
                    var blob = new Blob([CSV], { type: "text/csv;charset=utf-8;" });
                    navigator.msSaveBlob(blob, fileName + ".csv")
                }
            }
函数JSONTCSVCONVERTOR(JSONData、ReportTitle、ShowLabel){ //如果JSONData不是对象,那么JSON.parse将解析对象中的JSON字符串 var arrData=typeof JSONData!=“object”?JSON.parse(JSONData):JSONData; var CSV=“”; //在第一行或第一行设置报表标题 //CSV+=ReportTitle+'\r\n\n'; //此条件将生成标签/标题 如果(显示标签){ var行=”; //此循环将从数组的第一个索引中提取标签 对于(arrData[0]中的var索引){ //现在将每个值转换为字符串和逗号分隔符 行+=索引+','; } row=row.slice(0,-1); //用换行符追加标签行 CSV+=行+'\r\n'; } //第一个循环是提取每一行 对于(变量i=0;i>window.open(uri); //但这在某些浏览器中不起作用 //否则您将无法获得正确的文件扩展名 //这个技巧将生成一个临时标记 var link=document.createElement(“a”); link.href=uri; //将可见性设置为隐藏,使其不会影响web布局 link.style.visibility=“隐藏”; link.download=fileName+“.csv”; //此部件将附加定位标记,并在自动单击后将其删除 document.body.appendChild(链接); link.click(); document.body.removeChild(link); }否则{ var blob=new blob([CSV],{type:“text/CSV;charset=utf-8;”}); navigator.msSaveBlob(blob,文件名+“.csv”) } }
谢天谢地,经过几天的努力,我终于解决了这个问题。找到文件名问题的原因,我真的很惊慌。感谢我的队友,他在我感到失望后透露,这个问题是由谷歌分析引起的。由于我们只在生产环境中面临这个问题,而不是在localhost或dev测试站点上,并且Google分析代码具有生产条件,因此Google分析在localhost和dev测试环境中被忽略

有人建议使用encodeURI或encodeURIComponent而不是escape来编码csv字符串,我应用了它们,因为在我看来这是一个csv字符串编码问题不过,下面给出了对我有效的方法

link.target = "_blank";
这是如此简单和小,但逻辑,我没有注意到,我没有它在代码中,并发现它在其他人的帖子上的JSON到CSV转换代码


希望这对任何因“Google Analytics”而面临文件名问题的开发人员都有帮助

谢天谢地,经过几天的努力,我已经解决了这个问题。找到文件名问题的原因,我真的很惊慌。感谢我的队友,他在我感到失望后透露,这个问题是由谷歌分析引起的。由于我们只在生产环境中面临这个问题,而不是在localhost或dev测试站点上,并且Google分析代码具有生产条件,因此Google分析在localhost和dev测试环境中被忽略

有人建议使用encodeURI或encodeURIComponent而不是escape来编码csv字符串,我应用了它们,因为在我看来这是一个csv字符串编码问题但最终对我有效的是
let csvContent =  exampleDatas.map(e => e.join(",")).join("\n");
var link = document.createElement("a");
link.href = "data:text/csv;charset=utf-8,"+encodeURIComponent(csvContent);
link.download = "abc_def_ghi.csv";
link.click();
link.download = "abc_def_ghi.csv";