37

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.

Josh Bush
  • 2,708
  • 4
  • 24
  • 25

5 Answers5

62

Add a content-disposition to the header:

Response.AddHeader("content-disposition", @"attachment;filename=""MyFile.pdf""");
Dan F
  • 11,958
  • 3
  • 48
  • 72
Ryan Farley
  • 11,315
  • 4
  • 46
  • 43
  • 3
    Double quotes should be placed around the filename. See http://kb.mozillazine.org/Filenames_with_spaces_are_truncated_upon_download – Nathan Jones Sep 16 '08 at 16:33
24

FYI... if you use "inline" instead of "attachment" the file will open automatically in IE. Instead of prompting the user with a Open/Save dialogue.

Response.AppendHeader("content-disposition", string.Format("inline;FileName=\"{0}\"", fileName));
EMR
  • 396
  • 3
  • 6
  • This is exactly what i was looking for. The next problem is that i want to set a default file name so when they hit the save button it will have the right file name as the default. Right now the file name is set to the aspx page name that generated the pdf. Anyone got a suggestion? – Kevbo May 01 '17 at 17:05
15
Response.AppendHeader("Content-Disposition", "attachment; filename=foo.pdf");
Sklivvz
  • 30,601
  • 24
  • 116
  • 172
  • 5
    If anyone is wondering why there exists both `AddHeader` and `AppendHeader`, [wonder no more...](http://msdn.microsoft.com/en-us/library/system.web.httpresponse.addheader.aspx) – Michael Haren Jan 30 '12 at 23:39
  • how to make that file name as dynamic Response.AppendHeader("Content-Disposition", "attachment; filename="+ Projname + ".pdf");@Skliwz –  May 29 '15 at 06:20
2

For some reason, most of the answers out there don't seem to even attempt to encode the file name value. If the file contains spaces, semicolons or quotes, it mightn't come across correctly.

It looks like you can use the ContentDisposition class to generate a correct header value:

Response.AppendHeader("Content-Disposition", new ContentDisposition
{
    FileName = yourFilename
}.ToString());

You can check out the source code for ContentDisposition.ToString() to confirm that it's trying to encode it properly.

Warning: This seems to crash when the filename contains a dash (not a hyphen). I haven't bothered looking into this yet.

Sam
  • 40,644
  • 36
  • 176
  • 219
  • Dashes are frequently used in our file names, so that would be a significant drawback for us. When calling to the webpage concerned, I use HttpUtility.UrlEncode(FileName), I expect I could do the same here, if necessary. – Zeek2 Jul 27 '18 at 08:21
1
 Response.AddHeader("Content-Disposition", "attachment;filename=" & FileName & ";")
Kibbee
  • 65,369
  • 27
  • 142
  • 182