0

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");
    }
  • https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.fileresult?view=aspnet-mvc-5.2 – aardvark Jan 31 '20 at 16:46
  • 2
    Does this answer your question? [Returning a file to View/Download in ASP.NET MVC](https://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-asp-net-mvc) – Selim Yildiz Jan 31 '20 at 16:46
  • @SelimYıldız not sure... Post already shows exactly the same code as in the duplicate you've suggested. It looks like the question is more about what would happen when you do both - write to response (ASP.Net way) and return `ActionResult` (ASP.Net MVC way)… Tihomir - could you please clarify if you are looking for explanation of code posted OR proper way to return file in either ASP.Net or ASP.Net MVC? – Alexei Levenkov Jan 31 '20 at 17:04
  • @AlexeiLevenkov Thanks, I am looking for an explanation of what would happen, if I dont use return File(), I tried it and nothing seemed to go in any different way. – Tihomir Todorov Feb 01 '20 at 12:37
  • Since you closing response with Response.End only first way of returning the file actually impacts output... If you remove only one of the ways you send the file then the second one would continue to work. (Note that in ASP.Net `Response.End` would terminate the rendering, in ASP.Net MVC processing continues) – Alexei Levenkov Feb 01 '20 at 18:44
  • @AlexeiLevenkov Which way is more acceptable, removing the Response.Output.Write(csv) and .Response.ContentType and simply returning the file, or keeping and for example, returning a JSON? – Tihomir Todorov Feb 01 '20 at 19:12
  • Duplicate shows how to do it properly in ASP.Net MVC. If you have some concerns about it - ask new question. – Alexei Levenkov Feb 01 '20 at 19:45

0 Answers0