Pages

May 20, 2011

How to create a text file and save it on client machine using Save File Dialog + Asp.Net + C#

Method to Create text file:
Using System.IO;
Using SystemsText;

private void CreatetxtFile()
{
StringBuilder sb = new StringBuilder();

sb.AppendLine("test");
sb.AppendLine("= = = = = =");
sb.AppendLine();
sb.Append("test2");
sb.AppendLine();
sb.AppendLine();

using (StreamWriter outfile = new StreamWriter("c:" + @"\AllTxtFiles.txt"))
{
outfile.Write(sb.ToString());
}

}
Code to Save the file as dialog :
Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment; filename=AllTxtFiles.txt");
// specify the path of the file that has to be saved in client machine.
Response.TransmitFile("c://AllTxtFiles.txt");
Response.End();
The above code will show a save file dialog to save the file.

1 comment:

Anonymous said...

div id="blur" style="width: 100%;background-color: white;moz-opacity: 0.5;khtml-opacity: .5;
opacity: .5; filter: alpha(opacity=50);z-index: 120;height: 900px;position: absolute;top: 0;left: 0;"