Download text as a file from a webpage
<p>I'm downloading text from a website and I want to have the user be able to save it as a file. So, I have the following code that does just that.</p>

<pre><code>protected void DownloadFile(string fileName, string content)
{
    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
    Response.ContentType = "text/plain";
    Response.Write(content);
    Response.End();
}
</code></pre>

<p>The problem I am having is that I get an exception after this code runs. I believe it's due to the fact that I call Response.End(). So, every time a user downloads a file it redirects them to the generic error page because all generic exceptions redirect to that page.</p>

<p>Any ideas on how I can write text out to a file and not get this error? If I remove Response.End() I get my text and then the rest of the HttpResponse text, but I don't get the error. </p>

<p>Thanks.</p>
