I am trying to download some text output from the screen as a text file. Following is the code. It’s working on some pages and not working at all on other pages. Can anyone please suggest what’s wrong here?
protected void Button18_Click(object sender, EventArgs e){
Response.Clear();
Response.Buffer = true;
Response.ContentType = "text/plain";
Response.AppendHeader("content-disposition", "attachment;filename=output.txt");
StringBuilder sb = new StringBuilder();
string output = "Output";
sb.Append(output);
sb.Append("\r\n");
Response.Write(sb.ToString());
}
As already mentioned by Joshua, you need to write the text to the output stream (using
Responseobject). Also, don’t forget to invokeResponse.End()after that.Edit 1: added more details
Edit 2: I was reading other SO posts where users were recommending to put quotes around the filename:
Source:
https://stackoverflow.com/a/12001019/558486