I have some JavaScript that concatenates a CSV string. This string is then passed into an MVC action, which should open a file dialog asking the user to select the type of program they would like to open the CSV file with.
Originally, I was just using a vb StringBuilder to concatenate a string but I need to use js now. For some reason I can't get the same dialog that I had before, yet my MVC action has not changed. This is my JavScript that handles the concatenation and submits the string to the mvc action:
<script type="text/javascript">
$(document).ready(function () {
$("#csv-export").click(function () {
var csvStr = "";
$("#myTable tbody tr:visible").each(function () { //for each visible row
$(this).children("td").each(function () { //for each rows tds
if (!$(this).has("a").length) { //if the td does not contain a link
csvStr += $(this).html() + ", "; //Append the td's html
}
});
csvStr = csvStr.substring(0, csvStr.length - 2);
csvStr += "\n";
});
$.get('@Url.Action("CSVExport")', { csv: csvStr }, function (ReturnedData) {alert("Success!")});
});
});
</script>
I correctly receive the "Success!" alert on return.
Here is my MVC action, which correctly receives a string such as "field1, field2, field3 \n field1, field2, field3" note the line break in between each set of fields:
Public Function CSVExport(ByVal csv As String) As FileContentResult
If csv Is Nothing Then csv = "Sorry! There was an error creating this CSV file."
Return File(New System.Text.UTF8Encoding().GetBytes(csv), "text/csv", "Report123.csv")
End Function
But alas, no file dialog comes up. Is this a problem with my JavaScript/Action Return?