A file is being generated and downloaded from my action, what should I be returning in this case? I use the
Response.ContentType = "application/text";
Response.Output.Write(csv);
to make it download the file, whats the return in this case used for?
This is the whole action.
public ActionResult GenerateFieldApplicationCSVFile()
{
DataTable table = ApplicationHelper.GenerateTableWithApplicationExportColumns();
DataQueries.GenerateFieldApplicationCSVFile(table);
StringBuilder sb = new StringBuilder();
IEnumerable<string> columnNames = table.Columns.Cast<DataColumn>().
Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in table.Rows)
{
IEnumerable<string> fields = row.ItemArray.Select(field =>
string.Concat("\"", field.ToString().Replace("\"", "\"\""), "\""));
sb.AppendLine(string.Join(",", fields));
}
string csv = sb.ToString();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=records " + DateTime.Now + ".csv");
Response.Charset = "";
Response.ContentType = "application/text";
Response.Output.Write(csv);
Response.Flush();
Response.End();
return File(new System.Text.UTF8Encoding().GetBytes(csv), "application/text", "records " + DateTime.Now + ".csv");
}