Asp.Net News
Free Tech Magazines
Asp.Net Web Hosting
Suggest Us
Link to Us
Feeds Subscription
Questions & Answers
Tips & Tricks
 How to get the Content Type of the File Upload Dynamically?
Posted by Moderator1 on  6/28/2013 10:03:00 PM 2282 Views
There will be situation where we need to find the content type of the file we upload through an Asp.Net application. This is tedious to get dynamically. Below is the useful code that can be used to get the content type of the file.

C# Version:

public string GetContentType(string fileName)
string strcontentType = "application/octetstream";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (registryKey != null && registryKey.GetValue("Content Type") != null)
strcontentType = registryKey.GetValue("Content Type").ToString();
return strcontentType;

VB.NET Version:
Public Function GetContentType(fileName As String) As String
Dim strcontentType As String = "application/octetstream"
Dim ext As String = System.IO.Path.GetExtension(fileName).ToLower()
Dim registryKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext)
If registryKey IsNot Nothing AndAlso registryKey.GetValue("Content Type") IsNot Nothing Then
strcontentType = registryKey.GetValue("Content Type").ToString()
End If
Return strcontentType
End Function

Post New Tips/Tricks View all Tips/Tricks

Featured Resources:
The Beginner's Guide to Joomla
An Introduction to an Award-Winning Content Management System (CMS).
Everything You Know About CSS Is Wrong! - Free 37 Page Preview!
For web designers and developers who want to ensure they're using the very latest, best-practice CSS techniques.
100% Free Subscription until Stock lost.View complete list.
Sponsored by