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.GetMIMETyp e(), I've also tried using the HttpPostedFile and ASP.NET's FileUploader (it has a fileUploader.Ge tContentType(), but I can't access the fileuploader because it's all read only), but those didn't work.
Please help!
Thanks
- Q
5 21063 Plater 7,872
Recognized Expert Expert
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.
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 HttpFileCollect ion 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
There is a way of doing it using Internet Explorer's API. Please see...
http://www.pinvoke.net/default.aspx/urlmon.FindMime FromData
hth,
Matt
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_MACH INE\SOFTWARE\CL ASSES
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)
Looking for such a solution, I found this one http://kseesharp.blogspot.com/2008/0...file-name.html
private string GetMimeType(str ing fileName)
{
string mimeType = "applicatio n/unknown";
string ext = Path.GetExtensi on(fileName).To Lower();
RegistryKey regKey = Microsoft.Win32 .Registry.Class esRoot.OpenSubK ey(ext);
if (regKey != null && regKey.GetValue ("Content Type") != null)
mimeType = regKey.GetValue ("Content Type").ToString ();
return mimeType;
}
O.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 determine whether it's a gif, jpeg, png, etc?
--
http://www.sugapablo.com <--music ]
http://www.sugapablo.net <--personal ]
|
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
System.Runtime.InteropServices.COMException (0x80004005): The client
|
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 option to save the file. This is
the desired result. However, when I put the hyperlink on a webform, the
file is opened and it's binary contents are displayed on the screen. Does
anyone know why this happens?
The hyperlink is as follows:
|
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 button will verify the information that has been entered and
updates the data base if the data is correct. Update will throw an exception
if the data is not validate based on some given rules. I also have a custom
error handling page to show the...
|
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 I find a list of all the types which can be returned?
For example, if it is a Word document will it return "document/doc".
| |
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
|
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 able to make out if it's a zip file or a tar or a bz2
file.
|
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 the large size of the file it is
very slow. I have also been unable to access some of the relationships
contained in the document when using xsl. What would be the easiest way to
do this using visual C#? Below is a brief sample from one of the...
|
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
http://www.nathansokalski.com/
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |