FileType (identify MIME-type)

function FileType(filename: string): string;

Most file types can be identified regardless of the file name or extension, by simply reading the first 261 bytes (at most) of the file itself. Since there is no need to read the whole file, this process is extremely quick even for very large files, and doesn’t waste any RAM.

This function accepts the following parameters:

Parameter Type Requirement Explanation
filename string required must be a valid and fully qualified path to an existing file

This function returns a string containing the MIME-Type of the file, if it is identified. Here’s a list of file types that this function can identify:

Category Typical extension(s) Returned MIME-Type
Image jpg, jpeg image/jpeg
Image png image/png
Image gif image/gif
Image webp image/webp
Image cr2 image/x-canon-cr2
Image tif, tiff image/tiff
Image bmp, bmp2 image/bmp
Image heif image/heif
Image jxr image/vnd.ms-photo
Image psd image/vnd.adobe.photoshop
Image ico image/x-icon
Image dwg image/vnd.dwg
Video mp4 video/mp4
Video m4v video/x-m4v
Video mkv video/x-matroska
Video webm video/webm
Video mov, qt video/quicktime
Video avi video/x-msvideo
Video wmv video/x-ms-wmv
Video mpg video/mpeg
Video flv video/x-flv
Video 3gp video/3gpp
Audio mid audio/midi
Audio mp3 audio/mpeg
Audio m4a audio/m4a
Audio ogg audio/ogg
Audio flac audio/x-flac
Audio wav audio/x-wav
Audio amr audio/amr
Audio aac audio/aac
Archive epub application/epub+zip
Archive zip application/zip
Archive tar application/x-tar
Archive rar application/x-rar-compressed
Archive gz application/gzip
Archive bz2 application/x-bzip2
Archive 7z application/x-7z-compressed
Archive xz application/x-xz
Archive pdf application/pdf
Archive exe application/x-msdownload
Archive swf application/x-shockwave-flash
Archive rtf application/rtf
Archive iso application/x-iso9660-image
Archive eot application/octet-stream
Archive ps application/postscript
Archive sqlite application/x-sqlite3
Archive nes application/x-nintendo-nes-rom
Archive crx application/x-google-chrome-extension
Archive cab application/vnd.ms-cab-compressed
Archive deb application/x-deb
Archive ar application/x-unix-archive
Archive Z application/x-compress
Archive lz application/x-lzip
Archive rpm application/x-rpm
Archive elf application/x-executable
Archive dcm application/dicom
Document doc application/msword
Document docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
Document xls application/vnd.ms-excel
Document xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Document ppt application/vnd.ms-powerpoint
Document pptx application/vnd.openxmlformats-officedocument.presentationml.presentation
Font woff application/font-woff
Font woff2 application/font-woff
Font ttf application/font-sfnt
Font otf application/font-sfnt
Application wasm application/wasm