Skip to content

Instantly share code, notes, and snippets.

@roalcantara
Created May 10, 2012 23:22
Show Gist options
  • Save roalcantara/2656527 to your computer and use it in GitHub Desktop.
Save roalcantara/2656527 to your computer and use it in GitHub Desktop.
ASP.NET > Generate a TXT file in memory and download it
byte[] buffer;
using (var memoryStream = new System.IO.MemoryStream())
{
buffer = Encoding.Default.GetBytes("Sample text);
memoryStream.Write(buffer, 0, buffer.Length);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=filename.txt");
Response.AddHeader("Content-Length", memoryStream.Length.ToString());
Response.ContentType = "text/plain"; //This is MIME type
memoryStream.WriteTo(Response.OutputStream);
}
Response.End();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment