Any way to guess the mime type on Delphi XE2?

前端 未结 4 865
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-08 13:09

I need to guess the mime type with the purpose of fill the \"Content-Type\" header for some file uploads.

I fail to found a solution for it.

I wish to call somet

相关标签:
4条回答
  • 2021-02-08 13:18

    How many file types are you talking about? Maybe you can just create a list of types you want to support.

    I can imagine these lists to be available on the internet as well, for instance

    • here: http://www.webmaster-toolkit.com/mime-types.shtml
    • here: http://webdesign.about.com/od/multimedia/a/mime-types-by-file-extension.htm
    • here: List of ALL MimeTypes on the Planet, mapped to File Extensions?
    • here: http://hul.harvard.edu/ois/systems/wax/wax-public-help/mimetypes.htm

    Just get the file extension using ExtractFileExt and check it agains one of those lists. A file doesn't have a mime type in it, so you'll need to use either this list of file extensions, or determine the type by reading the file headers, which is harder and just as unreliable.

    0 讨论(0)
  • 2021-02-08 13:25

    Try using the FindMimeFromData Function.

    FindMimeFromData contains hard-coded tests for (currently 26) separate MIME types (see Known MIME Types). This means that if a given buffer contains data in the format of one of these MIME types, a test exists in FindMimeFromData that is designed (by scanning through the buffer contents) to recognize the corresponding MIME type.

    from urlmon.pas

    function FindMimeFromData(
        pBC: IBindCtx;                      // bind context - can be nil
        pwzUrl: LPCWSTR;                    // url - can be nil
        pBuffer: Pointer;                   // buffer with data to sniff - can be nil (pwzUrl must be valid)
        cbSize: DWORD;                      // size of buffer
        pwzMimeProposed: LPCWSTR;           // proposed mime if - can be nil
        dwMimeFlags: DWORD;                 // will be defined
        out ppwzMimeOut: LPWSTR;            // the suggested mime
        dwReserved: DWORD                   // must be 0
      ): HResult; stdcall;
    

    Also this article to see hot it works MIME Type Detection in Internet Explorer

    0 讨论(0)
  • 2021-02-08 13:29

    IE uses GetClassFileOrMime and FindMimeFromData API to detect the mime type of data/files (UrlMon unit in Delphi).

    MIME Type Detection in Internet Explorer

    The CLSID returned from GetClassFileOrMime could be located in the registry under HKEY_CLASSES_ROOT\CLSID\<clsid>\MimeTypes. (also FileExtensions, FriendlyName).

    The registry also holds a MIME database under: HKEY_CLASSES_ROOT\MIME\Database\Content Type.

    But since the list of known MIME types is relatively small you could hold that as a resource XML (or whatever) and simply fetch it from there. This will supprt both Windows and OSX.


    For file upload operation you can simply always use application/octet-stream.
    Indy has TIdMimeTable class (IdGlobal) and it uses a fixed list plus inspecting the registry HKEY_CLASSES_ROOT (see FillMimeTable). If no match is found application/octet-stream is used.
    You should probably want to inspect the file content at the server side once the file is uploaded, and not rely on the headers because the ContentType could be tampered at the client side, and also not match with the registry at the server-side.

    0 讨论(0)
  • 2021-02-08 13:30

    Internet Direct (Indy) contains a class for this:

    class TIdThreadSafeMimeTable

    in unit IdCustomHTTPServer

    Code example in a HTTP server application:

    Response.ContentType :=
      Response.HTTPServer.MIMETable.GetFileMIMEType(FileName);
    
    0 讨论(0)
提交回复
热议问题