views:

1212

answers:

3

How can I specify the filename when dumping data into the response stream?

Right now I'm doing the following:

byte[] data= GetFoo();
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/pdf";            
Response.BinaryWrite(data);
Response.End();

With the code above, I get "foo.aspx.pdf" as the filename to save. I seem to remember being able to add a header to the response to specify the filename to save.

+6  A: 
Response.AppendHeader("Content-Disposition", "attachment; filename=foo.pdf");
Sklivvz
+2  A: 

Add a content-disposition to the header:

Response.AddHeader("content-disposition", "attachment;filename=MyFile.pdf");
Ryan Farley
Double quotes should be placed around the filename. See http://kb.mozillazine.org/Filenames_with_spaces_are_truncated_upon_download
Nathan Jones
+1  A: 
 Response.AddHeader("Content-Disposition", "attachment;filename=" & FileName & ";")
Kibbee
That smells like VB syntax.
Dave Van den Eynde