\r\n characters in CSV file not treated as line breaks
<p>Apologies for what is surely a bonehead question...
I am trying to create a CSV file that looks like this:</p>

<p><code>Header1,Header2,Header3,
"Value1","Value2","Value3"</code></p>

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

<p><strong>How do I get my CRLF characters to actually produce line breaks in my output?</strong></p>

<p>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:</p>

<pre><code>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 &lt; table.Rows.Count; rowNum++) 
{
    for (int colNum = 0; colNum &lt; table.Columns.Count; colNum++)
    {
        result += "\"" + (table.Rows[rowNum][colNum]).ToString();
        result += "\",";
    };
    result += Environment.NewLine;
};
return result;
}
</code></pre>

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

<pre><code>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);
    }
</code></pre>

<p>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.</p>

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