Wednesday, November 2, 2011

Write text file in UTF8 format using MemoryStream

Below is the code to write text in UTF8 format using memorystream using c#.net
Response.Clear();
MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes("This is the text you want to write in text file"));
Response.AddHeader("Content-Disposition", "attachment; filename=filename.txt");          
Response.ContentType = "text/plain";
Response.ContentEncoding = System.Text.Encoding.UTF8;
memoryStream.WriteTo(Response.OutputStream);
Response.End();