8

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());
}
1
  • 4
    Could you solve someones problem if he would tell you that "it's working on some pages and not working at all on other pages" without telling you what's not working? Feb 7, 2013 at 15:57

2 Answers 2

31

As already mentioned by Joshua, you need to write the text to the output stream (Response). Also, don’t forget to invoke 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: added more details

Edit 2: I was reading other SO posts where users were recommending to put quotes around the filename:

Response.AppendHeader("content-disposition", "attachment;filename=\"output.txt\"");

Source: https://stackoverflow.com/a/12001019/558486

6
  • Thanks for your response, it's still not working with the above code. The file download dialog is not being invoked.
    – Vijay
    Feb 7, 2013 at 16:06
  • 1
    I would consider writing the data as binary as well.
    – Justin
    Feb 7, 2013 at 16:06
  • 2
    @RuiJarimba The button is in an UpdatePanel, when I removed the updatepanel, it worked.
    – Vijay
    Feb 7, 2013 at 16:24
  • this code works when used with <a href= ""/> but doesn't work with Button. Actually I am sending parameters with a URL so I have to use http get or post with button. Any idea? Jul 24, 2016 at 8:00
  • It's Write don't forget to use Response.End(); May 24, 2017 at 9:21
4

If that's your actual code, you never write the text to the response stream, so the browser never receives any data.

At the very least, you should need

Response.Write(sb.ToString());

to write your text data to the response. Also, as an added bonus, if you know the length beforehand you should provide it using the Content-Length header so the browser can show the download progress.

You're also setting Response.Buffer = true; as part of your method but never explicitly flush the response to send it to the browser. Try adding a Response.Flush() after your write statement.

2
  • Sorry, forgot to include that line in the code above. It's still not working. Thanks.
    – Vijay
    Feb 7, 2013 at 16:03
  • @Vj87 See my update, you're buffering so it might not be sending the content to the browser when you expect. Also, if this is a large amount of text, the entire file won't get sent to the browser until the server has buffered everything. You can also try setting Response.Buffer to false.
    – Joshua
    Feb 7, 2013 at 16:09

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.