Determining a file's MIME type using VB.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Queez
    New Member
    • Jul 2007
    • 24

    Determining a file's MIME type using VB.NET

    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
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    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.

    Comment

    • Queez
      New Member
      • Jul 2007
      • 24

      #3
      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
      Last edited by Queez; Oct 2 '07, 01:26 PM. Reason: Wroooong (HttpPostedFileCollection should be HttpFileCollection)

      Comment

      • nistrum
        New Member
        • Oct 2007
        • 1

        #4
        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

        Comment

        • Queez
          New Member
          • Jul 2007
          • 24

          #5
          Originally posted by nistrum
          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)

          Comment

          • OgeGOon
            New Member
            • Jul 2010
            • 1

            #6
            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.

            Comment

            Working...