I have found the following asp.net code to be very useful when serving files from a database:

Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);

This lets the user save the file to their computer and then decide how to use it, instead of the browser trying to use the file.

What other things can be done with the content-disposition response header?

share|improve this question
1  
There’s some nice documentation of this from Microsoft: support.microsoft.com/kb/260519 – Paul D. Waite Mar 31 '10 at 15:52
19  
Note that your sample code will break if filename contains whitespace or non-ASCII characters. See RFC 6266 for more information. – Julian Reschke Sep 2 '11 at 14:58
    
@JulianReschke, What about ASCII characters that are considered non-printable? (0 to 0x1F) – Pacerier Feb 5 '15 at 9:39
    
Read RFC 6266 (apart the fact they would be a bad idea to use in a filename; recipients are likely to throw them away anyway) – Julian Reschke Feb 5 '15 at 10:13
1  
@Ronnie Overby What is Content Disposition? – divy3993 Jun 23 '16 at 9:42
up vote 72 down vote accepted

The authority on the content-disposition header is RFC 1806 and RFC 2183. People have also devised content-disposition hacking. It is important to note that the content-disposition header is not part of the HTTP 1.1 standard.

The HTTP 1.1 Standard (RFC 2616) also mentions the possible security side effects of content disposition:

15.5 Content-Disposition Issues

RFC 1806 [35], from which the often implemented Content-Disposition
(see section 19.5.1) header in HTTP is derived, has a number of very
serious security considerations. Content-Disposition is not part of
the HTTP standard, but since it is widely implemented, we are
documenting its use and risks for implementors. See RFC 2183 [49]
(which updates RFC 1806) for details.

Note that RFC 6266 supersedes the RFCs referenced below. Section 7 outlines some of the related security concerns.

share|improve this answer
29  
Nowadays, the authority is RFC 6266. – Julian Reschke Sep 2 '11 at 14:57
16  
RFC 6266 can be found here: tools.ietf.org/html/rfc6266. – rstackhouse Jun 26 '13 at 15:09
    
@JulianReschke, How does "supersedes" and "updates" work? Does newer versions like RFC 7230 also renders RFC 6266 as obsolete? – Pacerier Dec 10 '14 at 12:17
    
@Pacerier -- why would RFC 7230 affect RFC 6266? – Julian Reschke Dec 10 '14 at 13:40
3  
Well, RFC 5678 here, RFC 9876 there. If Content-Disposition is frowned upon, what should we use instead? – Csaba Toth Mar 20 '16 at 18:46

Well, it seems that the Content-Disposition header was originally created for e-mail, not the web. (Link to relevant RFC.)

I'm guessing that web browsers may respond to

Response.AppendHeader("content-disposition", "inline; filename=" + fileName);

when saving, but I'm not sure.

share|improve this answer
3  
Only FF does; see greenbytes.de/tech/tc2231/#inlwithasciifilename – Julian Reschke Sep 2 '11 at 14:57
2  
I just tried with the latest IE and Chrome and it works now. – Traubenfuchs Jul 24 '14 at 14:59

Refer to RFC 6266 (Use of the Content-Disposition Header Field in the Hypertext Transfer Protocol (HTTP)) http://tools.ietf.org/html/rfc6266

share|improve this answer

For asp.net users, the .NET framework provides a class to create a content disposition header: System.Net.Mime.ContentDisposition

Basic usage:

var cd = new System.Net.Mime.ContentDisposition();
cd.FileName = "myFile.txt";
cd.ModificationDate = DateTime.UtcNow;
cd.Size = 100;
Response.AppendHeader("content-disposition", cd.ToString());
share|improve this answer
1  
Beware, this class does not conform to RFC 6266. It does UTF-8 base64 encoding in filename parameter, instead of using filename* parameter with RFC 5987 encoding. No way to derive or use fx utils to fix that, almost everything is non-overridable or internal... .Net fx has still a long way learning openness and extensibility. In MVC 5.2, FileResult class does a little better for filename, but does not handles others parameters as inline and most of its implementation is also internal... – Frédéric Jan 19 '16 at 9:39

This header is defined in RFC 2183, so that would be the best place to start reading.

Permitted values are those registered with the Internet Assigned Numbers Authority (IANA); their registry of values should be seen as the definitive source.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.