Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to guess the mime type on Delphi XE2?

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 something like: GetMimeType('C:File.jpg') and get back image/jpg.

Best if is multiplataform (for win/osx) but will be enough if only for windows.

like image 878
mamcx Avatar asked Mar 24 '12 22:03

mamcx


People also ask

How would you determine the MIME type of a file?

The application uses file content sniffers to search for a particular pattern in the file. A file content sniffer associates a specific pattern in a file with a MIME type. If the application finds a match for the pattern, the MIME type associated with the pattern is the MIME type of the file.

How do I specify a MIME type?

In the Connections pane, go to the site, application, or directory for which you want to add a MIME type. In the Home pane, double-click MIME Types. In the MIME Types pane, click Add... in the Actions pane. In the Add MIME Type dialog box, add the file name extension and MIME type, and then click OK.

Which of these are MIME types?

A MIME type consists of two parts: a type and a subtype. Currently, there are ten registered types: application, audio, example, font, image, message, model, multipart, text, and video.

What is the default MIME type?

Two primary MIME types are important for the role of default types: text/plain is the default value for textual files. A textual file should be human-readable and must not contain binary data. application/octet-stream is the default value for all other cases.


1 Answers

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

like image 159
RRUZ Avatar answered Sep 26 '22 23:09

RRUZ