I am trying to write a string that has multiple lines (comes from the server side) into a csv file for user to download in browser. However, using my code, I only get the csv files with all the data in a single line. Here is my code:
function getReport(){
var report = "a,b,c,d;1,2,3,4;";
//console.log(report);
var csvcontent="";
while (report.indexOf(";")!=-1)
{
csvcontent=csvcontent+ report.substring(0,report.indexOf(";"))+"\n";
report=report.substring(report.indexOf(";")+1);
}
console.log(csvcontent);
var a = document.createElement('a');
a.href = 'data:attachment/csv,' + csvcontent;
a.target = '_blank';
a.download = 'myFile.csv';
document.body.appendChild(a);
//console.log("ok");
a.click();
}
In the downloaded csv file, all the data will in a single line a,b,c,d1,2,3,4. Can anyone tell me how to solve this problem? Thanks in advance!