nwhaught nwhaught - 3 months ago 22
C# Question

\r\n characters in CSV file not treated as line breaks

Apologies for what is surely a bonehead question...
I am trying to create a CSV file that looks like this:

Header1,Header2,Header3,
"Value1","Value2","Value3"


Instead, I'm getting a CSV file that looks like this:
Header1,Header2,Header3,\r\n"Value1","Value2","Value3"


How do I get my CRLF characters to actually produce line breaks in my output?

What I'm doing is making an ajax call to a WebMethod that generates a datatable from a stored proc. That datatable is then parsed out to a CSV like so:

if (!createHeaders)//I deal with the headers elsewhere in the code
{
foreach(DataColumn col in table.Columns){

result += col.ColumnName + ",";
};
result += Environment.NewLine;
}

for (int rowNum = 0; rowNum < table.Rows.Count; rowNum++)
{
for (int colNum = 0; colNum < table.Columns.Count; colNum++)
{
result += "\"" + (table.Rows[rowNum][colNum]).ToString();
result += "\",";
};
result += Environment.NewLine;
};
return result;
}


That string is then passed back to the success function of the ajax query, where it undergoes a few more transformations...

function getExportFile(sType) {
var alertType = null
$.ajax({
type: "POST",
url: "./Services/DataLookups.asmx/getExcelExport",
data: JSON.stringify({ sType: sType }),
processData: false,
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function (response) {
response = replaceAll(response,"\\\"", "\"")
response = response.replace("{\"d\":\"", "")
response = response.replace("\"}", "")
download(sType + ".csv",response)
}
});

function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click()
document.body.removeChild(element);
}

function escapeRegExp(str) {
return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}

function replaceAll(str, find, replace) {
return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}


In my debugging before the string is passed back to javascript, if I just type ?response, I get the incorrect, all-on-one-line response. However, when I type ?resonse,nq the linebreaks are recognized, and everything looks the way it should.

Also, I'm sure there is PLENTY that I'm doing wrong/stupidly here. Pointing out those instances is also appreciated.

Answer

Your header should have data:Application/octet-stream, as MIME-type instead of data:text/plain;charset=utf-8,, the reason being that as per HTTP specifications, when the content type is unknown, the recipient should treat it as type application/octet-stream.

Since you are already using the encodeURIComponent(), that seems to be the only issue remaining.

Comments