<p>How can I specify the filename when dumping data into the response stream?</p>

<p>Right now I'm doing the following:</p>

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

<p>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.</p>

[{'answer_id': 74044, 'author': 'Sklivvz', 'author_id': 7028, 'author_profile': 'https://Stackoverflow.com/users/7028', 'pm_score': 4, 'selected': False, 'text': '<pre><code>Response.AppendHeader("Content-Disposition", "attachment; filename=foo.pdf");\n</code></pre>\n'}, {'answer_id': 74049, 'author': 'Ryan Farley', 'author_id': 1627, 'author_profile': 'https://Stackoverflow.com/users/1627', 'pm_score': 7, 'selected': True, 'text': '<p>Add a content-disposition to the header:  </p>\n\n<pre><code>Response.AddHeader("content-disposition", @"attachment;filename=""MyFile.pdf""");\n</code></pre>\n'}, {'answer_id': 74060, 'author': 'Kibbee', 'author_id': 1862, 'author_profile': 'https://Stackoverflow.com/users/1862', 'pm_score': 1, 'selected': False, 'text': '<pre><code> Response.AddHeader("Content-Disposition", "attachment;filename=" &amp; FileName &amp; ";")\n</code></pre>\n'}, {'answer_id': 14347162, 'author': 'EMR', 'author_id': 729980, 'author_profile': 'https://Stackoverflow.com/users/729980', 'pm_score': 5, 'selected': False, 'text': '<p>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.</p>\n\n<pre><code>Response.AppendHeader("content-disposition", string.Format("inline;FileName=\\"{0}\\"", fileName));\n</code></pre>\n'}, {'answer_id': 30972670, 'author': 'Sam', 'author_id': 238753, 'author_profile': 'https://Stackoverflow.com/users/238753', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n\n<p>It looks like you can use the <a href="http://msdn.microsoft.com/library/system.net.mime.contentdisposition.aspx" rel="nofollow"><code>ContentDisposition</code></a> class to generate a correct header value:</p>\n\n<pre><code>Response.AppendHeader("Content-Disposition", new ContentDisposition\n{\n    FileName = yourFilename\n}.ToString());\n</code></pre>\n\n<p>You can check out <a href="http://referencesource.microsoft.com/System/net/System/Net/mail/ContentDisposition.cs.html#234ee68565525ad0" rel="nofollow">the source code for <code>ContentDisposition.ToString()</code></a> to confirm that it\'s trying to encode it properly.</p>\n\n<p><strong>Warning</strong>: This seems to crash when the filename contains a dash (not a hyphen). I haven\'t bothered looking into this yet.</p>\n'}]
['https://Stackoverflow.com/questions/74019', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1672/']