Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the standard file extension given a MIME type in Delphi?

Is there a built-in way to get the standard extension of a given MIME type in Delphi (XE7)?

I'm looking for the easiest and most general way to implement a function that would be called like this:

fileExt := GetExtension('text/xml');
like image 204
Helmut D Avatar asked Dec 10 '22 10:12

Helmut D


1 Answers

It seems Indy has a built in function for that, on TIdThreadSafeMimeTable:

Uses
  IdCustomHTTPServer;


function GetMIMETypeDefaultExtension(const aMIMEType: String): String;
var
  mimetable: TIdThreadSafeMimeTable;
Begin
  if not(aMIMEType.Trim.IsEmpty) then
  Begin
    mimetable := TIdThreadSafeMimeTable.Create(true);
    try
      result := mimetable.GetDefaultFileExt(aMIMEType);
    finally
      mimetable.Free;
    end
  End
  else
      result := '';
End;

Edit: function fixed to use TIdThreadSafeMimeTable directly without custom http server.

like image 181
Tuncay Göncüoğlu Avatar answered Dec 24 '22 00:12

Tuncay Göncüoğlu