0

I am using the below code to export data to csv format. Normally its stored into the system desktop. i want to save various location thats why need save dialog. But i can't do this please help me to do it..

My partial code is here :

       StringBuilder sb = new StringBuilder();
        var columnNames = dt.Columns.Cast<DataColumn>().Select(column => column.ColumnName).ToArray();
        sb.AppendLine(string.Join(",", columnNames));
        foreach (DataRow row in dt.Rows)
        { var fields = row.ItemArray.Select(field => field.ToString().Replace(","," "));
        sb.AppendLine(string.Join(",", fields));
        }
        File.WriteAllText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) +"/test.csv", sb.ToString()); 
Bharathi
  • 33
  • 3
  • 2
    You are aware that ASP.NET runs on a server, and not on your own computer (unless of course on your local IIS)? – Styxxy Jun 19 '12 at 13:37

1 Answers1

0

If you need to open up a Save Dialog, you need to add the content-disposition header to your response as so:

Response.AddHeader("content-disposition", "attachment;filename=test.csv");

Now you can write the string to the response:

Response.Write(sb.ToString());
Response.End(); //VERY IMPORTANT
Icarus
  • 63,293
  • 14
  • 100
  • 115