0

Is there a way I can automatically generate Content-Type headers in .NET?

I'm working on a system where users upload files in many formats and the only meta-data available to me is the file extension.

One of my forms has a link to an ashx resource that writes files to the response. I would prefer to set the appropriate content type header so the browser knows how to deal with the file if the user wants to open it directly instead of saving it.

If there isn't anything prefab available I'll have to start making my own compilation of extension to content type header mappings, but I'd prefer not having to do unnecessary work.

This answer has something similar for Java, but I couldn't find anything for .NET

Community
  • 1
  • 1
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • Maybe something like http://stackoverflow.com/a/1910179/23354 ? Or just have your own custom list somewhere? – Marc Gravell Nov 27 '12 at 08:05
  • @MarcGravell The registry option is unreliable, because the server doesn't have Office etc, which means the corresponding MIME entries aren't in the registry. I ended up writing a parser for this list http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types – Asad Saeeduddin Nov 27 '12 at 08:08
  • possible duplicate of [How do I determine a file's content type in .NET?](http://stackoverflow.com/questions/691212/how-do-i-determine-a-files-content-type-in-net) – Asad Saeeduddin May 23 '13 at 18:43

1 Answers1

0

I ended up using this file to fill a table with MIME type to extension mappings, and my script refers to it whenever it is serving up files. Works well enough.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139