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
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\
. (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.