Determining ContentType for binary attachments

  • Thread starter Thread starter Stephen Walch
  • Start date Start date
S

Stephen Walch

I have seen many examples on the web for downloading attachments ffrom
ASP.NET forms using response.WriteBinary, but they all use hard-coded
ContentTypes (for example, Response.ContentType = "application/pdf"). Is
there a good way to "guess" the content type for arbritary file name? Every
web server has a mapping from file extension to content type, but how do I
get this from ASP.NET? Thanks!
 
Could use a huge CASE statement with the extensions for the files and the
content type normally associated.
 
Hi Stephen,

In addition to Curt_C's suggestion on use a huge CASE to determine the
file's contenttype accroding to the filename extention, you can also
consider use the 'application/octet-stream' as a common value if you're not
sure about the exact MIME TYPE of your filestream. Here is the msdn
reference on how IE browser detect the download stream's content Type:

http://msdn.microsoft.com/library/default.asp?url=/workshop/networking/monik
er/overview/appendix_a.asp

Hope also helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Back
Top