As Joshua already mentioned, you need to write the text in the output stream (Response). Also, be sure to call Response.End () after that.
protected void Button18_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); string output = "Output"; sb.Append(output); sb.Append("\r\n"); string text = sb.ToString(); Response.Clear(); Response.ClearHeaders(); Response.AppendHeader("Content-Length", text.Length.ToString()); Response.ContentType = "text/plain"; Response.AppendHeader("Content-Disposition", "attachment;filename=\"output.txt\""); Response.Write(text); Response.End(); }
Edit 1: more details added
Edit 2: I read other SO posts in which users recommend putting quotes around the file name:
Response.AppendHeader("content-disposition", "attachment;filename=\"output.txt\"");
Source: fooobar.com/questions/937088 / ...
source share