473,395 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Determining a file's MIME type using VB.NET

24
Afternoon,

I've spent all morning trawling the net looking for an answer to this. In the end, I've had to resort to good old fashioned asking.

How do you determine a file's MIME type using VB.NET?

The problem is:
- I have an ASP.NET web application
- I can display a list of all files in a directory on the server (it's a "directory share" for a scanner)
- I want the user to be able to select one of the files to import into the database
- I need to be able to determine the MIME type of the selected file so I can store and use it later

I've looked around the various IO.FileInfo, IO.File, IO.FileStream etc. for something like File.GetMIMEType(), I've also tried using the HttpPostedFile and ASP.NET's FileUploader (it has a fileUploader.GetContentType(), but I can't access the fileuploader because it's all read only), but those didn't work.

Please help!

Thanks

- Q
Oct 2 '07 #1
5 20951
Plater
7,872 Expert 4TB
You'll probably have to implement a lookup table.
Match extensions to mime-types (the way your system does)

Key | Value
-------------------
"jpg" | "image/jpg"


And so on.
Oct 2 '07 #2
Queez
24
Haha, I was really hoping you weren't going to say that.

I guess when you call the HttpPostedFile.ContentType property, ASP.NET must have some kind of built-in MIME type lookup table, I was hoping there was another way to access this.

I suppose there may be some way of fooling ASP.NET into using the HttpPostedFile class, as this would make everything much easier. I'm going to try adding my file into the HttpFileCollection and see how far I get.

Anyway, thanks for your prompt reply, Plater. I'll look into this some more and post my findings back here if I come up with anything!

Cheers

- Q
Oct 2 '07 #3
There is a way of doing it using Internet Explorer's API. Please see...

http://www.pinvoke.net/default.aspx/urlmon.FindMimeFromData

hth,
Matt
Oct 9 '07 #4
Queez
24
There is a way of doing it using Internet Explorer's API.
Thanks Nistrum, that looks like it would work. In the end though, I've used a user-maintainable list of acceptable file types. This allows an administrator of the system to limit the types of file available to upload and thus gives the administrators much more control. If they wish to be able to add a new file type, all they gotta do is add it to the list.

I did find the location in the system registry where it would be possible to obtain the MIME types given the file extension, I am considering using this to help confirm the administrator's choice of filetype/extension:

HKEY_LOCAL_MACHINE\SOFTWARE\CLASSES

Most of the top class entries contain a "Content Type" which is the MIME type for that extension (e.g. for .avi, ContentType = video/avi)
Oct 9 '07 #5
Looking for such a solution, I found this one http://kseesharp.blogspot.com/2008/0...file-name.html

private string GetMimeType(string fileName)
{
string mimeType = "application/unknown";
string ext = Path.GetExtension(fileName).ToLower();
RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ex t);
if (regKey != null && regKey.GetValue("Content Type") != null)
mimeType = regKey.GetValue("Content Type").ToString();
return mimeType;
}

O.
Jul 15 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Sugapablo | last post by:
Is there any PHP function that will allow me to determine the MIME type of a blob stored in MySQL? Specifically, if I'm storing an image as a blob in MySQL, is there any PHP function that can...
0
by: CTDev Team | last post by:
Hi, We are using Exchange Server 5.5, and have applications written in VB6 and C# that read and process emails. We are experiencing intermittent errors similar to C# Application ...
5
by: JerryK | last post by:
Hi, I am trying to put a hyperlink to a .cer (certificate) file on a form. Normally (on a ,htm page) when the user clicks on the hyperlink, the file is not recognized and the user is given the...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
3
by: Shapper | last post by:
Hello, I created a script to upload a file. To determine the file type I am using userPostedFile.ContentType. For example, for a png image I get "image/png". My questions are: 1. Where can...
1
by: Mantorok | last post by:
Hi Is there a "concrete" way of determining the Mime type? I'm currently using the registry as a source, however, what if the extension is not listed in the registry? Thanks Kev
8
by: Ritesh Raj Sarraf | last post by:
Hi, I have a funtion named unzipper() which does the work of unzipping the files. Is there a way I can identify what is the type of the file which'll be passed to unzipper(). If yes, I'll be...
3
by: ccole | last post by:
I have about thirteen configuration files that are written in XML. I need to be able to read and cross reference these files with each other. I have already written an xsl translator but due to...
1
by: Nathan Sokalski | last post by:
I am having trouble determining the MIME type of a file. Does anybody know how to do this for a file programmatically? Thanks. -- Nathan Sokalski njsokalski@hotmail.com...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.