I'm trying to write to a text file in memory and then download that file without saving the file to the hard disk. I'm using the StringWriter
to write the contents:
StringWriter oStringWriter = new StringWriter();
oStringWriter.Write("This is the content");
How do I then download this file?
EDIT:
It was combination of answers which gave me my solution. Here it is:
StringWriter oStringWriter = new StringWriter();
oStringWriter.WriteLine("Line 1");
Response.ContentType = "text/plain";
Response.AddHeader("content-disposition", "attachment;filename=" + string.Format("members-{0}.csv",string.Format("{0:ddMMyyyy}",DateTime.Today)));
Response.Clear();
using (StreamWriter writer = new StreamWriter(Response.OutputStream, Encoding.UTF8))
{
writer.Write(oStringWriter.ToString());
}
Response.End();
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…