MIME Type Verification of Document and Image Files using LEADTOOLS

While working on a web application that accepts file uploads as an input, I found myself working pretty extensively with many different MIME types. Also known as Media types, a MIME type is a two part identifier that simply indicates the nature and format of a document, file, or assortment of bytes. While trying to make the web app secure, I started using the MIME type to verify that the file being uploaded was safe and able to be processed.

The problem was that the browser couldn’t recognize the MIME type of certain files such as JPX or BMP and would simply return application/octet-stream as the media type which indicates that the body contains arbitrary binary data. Processing unknown binary data is a major security flaw, especially when the input could be from an unknown sender. Thankfully, because LEADTOOLS supports a variety of over 165 file formats, I was able to use the LEADTOOLS SDK to check and verify the file’s format and the MIME type to ensure it was an allowed type to be processed. I did this in an out-of-process executable to ensure that the file was quarantined until it was deemed safe. Below is a small sample application I created that gives the basics behind this solution.

Full MIME Type Verification Application in Just 4 Lines of Code!

You read that right! The sample below will get the MIME type of any document or image file in just four lines of code.


private static void Main(string[] args)
        {
            //this sets the license file for the LEADTOOLS SDK
            InitLEAD();
            var fileDir = "C:\\LEADTOOLS22\\Resources\\Images\\FileSamplesForMimetype";
            foreach(var file in Directory.GetFiles(fileDir))
            {
                GetMimeType(file);
            }
        }
static void GetMimeType(string file)
        {
            using var codecs = new RasterCodecs();
            //using RasterCodecs we can pull the information out of the file
            var info = codecs.GetInformation(file, false);
            //we use the information we pulled out and help find the one we need
            var mimeType = RasterCodecs.GetMimeType(info.Format);
            //we then display the MimeType to the console
            Console.WriteLine(mimeType.ToString());
        }

When all is said and done, the console log should look something like this!

Output of the application running that shows the mimetypes of each file.

Build for Yourself!

Here is a zip folder containing the files shown in this demo: FileSamplesForMimetype. You can download our FREE 60-day evaluation SDK to test all of our features and actually program with LEADTOOLS. You’ll also receive access to our fully-sourced demos, tutorials, and more!

Not sure where to start? Schedule a walk-through with our support team to check out our many features that will expedite your application development!

For pricing or licensing questions, contact our sales team via email or call us at 704-332-5532. Be sure to check out our product wizard and pricing estimator as well.

About 

This entry was posted in Document Imaging and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *