I should start by saying that I am using ASP.NET using C# in a .NET 2.0 environment. In particular, I am trying to generate a csv download when the user clicks on a link button. The link to my postback is inside an UpdatePanel. In the past (before ajax) to return non-html content I would use code such as the following:
string filename = e.CommandArgument.ToString();//somefile.csv string fileContents = SomeClass.GetFile(filename); Response.AddHeader('Content-disposition', string.Format('attachment; filename={0}', filename)); Response.Write(fileContents);
But since the content is not trying to do a full refresh of the browser this technique does not work.
Does someone have a better approach for this kind of situation. One constraint I have is that I am stuck with .net 2.0 for this part of the project and can’t switch to 3.5 to solve this problem.
p.s. I need to generate the content with a dynamic filename as well
I would avoid generating a postback for this at all. Istead, you want to use a completely separate page, preferably even just a handler (*.ashx). Then you can use a normal hyperlink and your old attachment code will work just fine.