user170386
user170386

Reputation:

ASP.net text file download generation

I tried to look this up all over the place, but none of the examples given online helps me.

I basically want a button on an ASP.net page to generate some text in memory, and when the user clicks on it, display a download dialog for a user to download a text file with the generated text.

The code I have currently is:

Response.Clear();
Response.ClearHeaders();

Response.ContentType = "text/csv";
Response.AppendHeader("Content-Disposition", "attachment;filename=cartune.csv");

string text = "hello";

Response.Write(text);
Response.End();

But it doesn't work in Firefox 3.5 nor IE 8.0, I get no download response from the browsers at all... Many examples given online are similar to the code I have, so can any experts please point out to me what I am doing wrong?

I am running debugging mode in the Visual Studio 2008 deubugger, not IIS.

Thanks!

Upvotes: 1

Views: 1600

Answers (1)

Martin
Martin

Reputation: 11041

    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    Response.AddHeader("content-disposition", "attachment;filename=filename.csv");
    Response.ContentType = "text/csv";
    Response.Write("hello");
    Response.End();

Works for me in Firefox 3.5. I just wrote this for my own project today.

Upvotes: 3

Related Questions