Change the name of the file sent to the client?

I have a webpage that retrieves information from a database, converts it to .csv format, and writes the file to HTTPResponse.

string csv = GetCSV(); Response.Clear(); Response.ContentType = "text/csv"; Response.Write(csv); 

This works great and the file is sent to the client without any problems. However, when the file is sent to the client, the name of the current page is used, and not a friendlier name (for example, "data.csv").

alt text

My question is, how can I change the name of the file that is written to the output stream without writing the file to disk and redirecting the client to the file URL?

EDIT: Thanks for the answers to the guys. I received 4 identical answers, so I chose only the first as the answer.

+6
source share
4 answers

I believe this will work for you.

 Response.AddHeader("content-disposition", "attachment; filename=NewFileName.csv"); 
+11
source

You just need to set the Content-Disposition header.

 Content-Disposition: attachment; filename=data.csv 

There is good information in this Microsoft support article.

How to raise the File Download dialog box for a known MIME type

+2
source

Add a "Content-Disposition" header with the value "attachment; filename = filename.csv".

+1
source
 Response.AddHeader("content-disposition", "attachment; filename=File.doc") 
+1
source

All Articles