none
How do you get the correct file mime types in .net? RRS feed

  • Question

  • Is it just as simple as comparing the file extension and searching online for the correct type, e.g. Content-Type: application/msword ?
    Sunday, February 23, 2020 2:59 PM

Answers

  • If you're using >= .Net v4.5 then go for System.Web.MimeMapping.GetMimeMapping(param) entering your file name into the param. This will give you the correct mime type
    • Marked as answer by Brian Wyse Friday, March 6, 2020 4:27 PM
    Tuesday, February 25, 2020 1:21 PM

All replies

  • You can download a list of mime-types.  Mime-types have nothin to do with file extensions.  They de3clare file content.

    Start by reading what mime-types are and you will understaned how to use them.

    https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types

    Mime-types are about transferring and identifying content and no files.   They can be used to identify the file content of a file being transferred via web or stored in an XML document.  Different systems are under no obligation to recognize file extensions.  That is up to the system builders.  Mime-Type provides a system independent method for identifying the content of a piece of data or a file that has been transferred.


    \_(ツ)_/


    • Edited by jrv Sunday, February 23, 2020 4:25 PM
    Sunday, February 23, 2020 4:22 PM
  • For others with similar issues here is the root of all names used on the Internet: IANA.  This is what makes teh modern Internet tick at a global level. It i where we define everything "Internet" and not just web.

    International Assigned Numbers Authority

    I am sure that IANA has a SOAP API to use to look up strings associated with protocol assignments.  You can also download the list of "Media Types" to get the current registry.

    Awareness and familiarity with IANA is critical for anyone working in computer technology.

    The following page is the best place to catch up.  It also contains links to associated services that maintain global definitions of Internet/Computer definitions and assignments.  Take ample time to digest this single and quite short page: https://en.wikipedia.org/wiki/Internet_Assigned_Numbers_Authority


    \_(ツ)_/

    Sunday, February 23, 2020 4:42 PM
  • If you're using >= .Net v4.5 then go for System.Web.MimeMapping.GetMimeMapping(param) entering your file name into the param. This will give you the correct mime type
    • Marked as answer by Brian Wyse Friday, March 6, 2020 4:27 PM
    Tuesday, February 25, 2020 1:21 PM
  • If you're using >= .Net v4.5 then go for System.Web.MimeMapping.GetMimeMapping(param) entering your file name into the param. This will give you the correct mime type

    Unfortunately that only works in Windows for known file associations.  It is useful for setting type when transmitting files.  All registered mime type are not accounted for but it can be useful.  Since Mime is about content and this only checks file extension then it can easily be tricked.

    PS C:\scripts> add-type -AssemblyName System.Web
    PS C:\scripts> [System.Web.MimeMapping]::GetMimeMapping('test.txt')
    text/plain
    PS C:\scripts> [System.Web.MimeMapping]::GetMimeMapping('test.xml')
    text/xml
    PS C:\scripts>

    On all unknown types it will always return "application/octet-stream".


    \_(ツ)_/

    Tuesday, February 25, 2020 4:05 PM
  • Here is the source for the class.  You can see that a very small number of Mime Types are hard coded into the class.

    http://www.dotnetframework.org/default.aspx/FXUpdate3074/FXUpdate3074/1@1/untmp/whidbey/QFE/ndp/fx/src/xsp/System/Web/MimeMapping@cs/1/MimeMapping@cs


    \_(ツ)_/

    Tuesday, February 25, 2020 4:07 PM