Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8550519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:58:03+00:00 2026-06-11T13:58:03+00:00

How do I check the file type of a file uploaded using FileUploader control

  • 0

How do I check the file type of a file uploaded using FileUploader control in an ASP.NET C# webpage?

  1. I tried checking file extension, but it obviously fails when a JPEG image (e.g. Leonardo.jpg) is renamed to have a PDF’s extension (e.g. Leonardo.pdf).

  2. I tried

    FileUpload1.PostedFile.ContentType.ToLower().Equals("application/pdf")
    

    but this fails as the above code behaves the same way as the first did.

Is there any other way to check the actual file type, not just the extension?

I looked at ASP.NET how to check type of the file type irrespective of extension.

Edit: I tried below code from one of the posts in stackoverflow. But this down’t work. Any idea about this.

/// <summary>
/// This class allows access to the internal MimeMapping-Class in System.Web
/// </summary>
class MimeMappingWrapper
{
  static MethodInfo getMimeMappingMethod;

    static MimeMappingWrapper() {
    // dirty trick - Assembly.LoadWIthPartialName has been deprecated
    Assembly ass = Assembly.LoadWithPartialName("System.Web");
    Type t = ass.GetType("System.Web.MimeMapping");

    getMimeMappingMethod t.GetMethod("GetMimeMapping", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public));
}

/// <summary>
/// Returns a MIME type depending on the passed files extension
/// </summary>
/// <param name="fileName">File to get a MIME type for</param>
/// <returns>MIME type according to the files extension</returns>
public static string GetMimeMapping(string fileName) {
    return (string)getMimeMappingMethod.Invoke(null, new[] { fileName });
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:58:05+00:00Added an answer on June 11, 2026 at 1:58 pm

    Dont use File Extensions to work out MIME Types, instead use “Winista” for binary analysis.

    Say someone renames an exe with a jpg extension. You can still determine the real file format. It doesn’t detect swf’s or flv’s but does pretty much every other well known format and you can get a hex editor to add more files it can detect.

    Download Winista: here or my mirror or my GitHub https://github.com/MeaningOfLights/MimeDetect.

    Where Winista fails to detect the real file format, I’ve resorted back to the URLMon method:

    public class urlmonMimeDetect
    {
        [DllImport(@"urlmon.dll", CharSet = CharSet.Auto)]
        private extern static System.UInt32 FindMimeFromData(
            System.UInt32 pBC,
            [MarshalAs(UnmanagedType.LPStr)] System.String pwzUrl,
            [MarshalAs(UnmanagedType.LPArray)] byte[] pBuffer,
            System.UInt32 cbSize,
            [MarshalAs(UnmanagedType.LPStr)] System.String pwzMimeProposed,
            System.UInt32 dwMimeFlags,
            out System.UInt32 ppwzMimeOut,
            System.UInt32 dwReserverd
        );
    
    public string GetMimeFromFile(string filename)
    {
        if (!File.Exists(filename))
            throw new FileNotFoundException(filename + " not found");
    
        byte[] buffer = new byte[256];
        using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
        {
            if (fs.Length >= 256)
                fs.Read(buffer, 0, 256);
            else
                fs.Read(buffer, 0, (int)fs.Length);
        }
        try
        {
            System.UInt32 mimetype;
            FindMimeFromData(0, null, buffer, 256, null, 0, out mimetype, 0);
            System.IntPtr mimeTypePtr = new IntPtr(mimetype);
            string mime = Marshal.PtrToStringUni(mimeTypePtr);
            Marshal.FreeCoTaskMem(mimeTypePtr);
            return mime;
        }
        catch (Exception e)
        {
            return "unknown/unknown";
        }
    }
    }
    

    From inside the Winista method, I fall back on the URLMon here:

       public MimeType GetMimeTypeFromFile(string filePath)
        {
            sbyte[] fileData = null;
            using (FileStream srcFile = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                byte[] data = new byte[srcFile.Length];
                srcFile.Read(data, 0, (Int32)srcFile.Length);
                fileData = Winista.Mime.SupportUtil.ToSByteArray(data);
            }
    
            MimeType oMimeType = GetMimeType(fileData);
            if (oMimeType != null) return oMimeType;
    
            //We haven't found the file using Magic (eg a text/plain file)
            //so instead use URLMon to try and get the files format
            Winista.MimeDetect.URLMONMimeDetect.urlmonMimeDetect urlmonMimeDetect = new Winista.MimeDetect.URLMONMimeDetect.urlmonMimeDetect();
            string urlmonMimeType = urlmonMimeDetect.GetMimeFromFile(filePath);
            if (!string.IsNullOrEmpty(urlmonMimeType))
            {
                foreach (MimeType mimeType in types)
                {
                    if (mimeType.Name == urlmonMimeType)
                    {
                        return mimeType;
                    }
                }
            }
    
            return oMimeType;
        }
    

    Update:

    To work out more files using magic here is a FILE SIGNATURES TABLE

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I am using ctype_alnum to check for alphanumeric type in csv file. But
In php I can check if a uploaded file has proper type by extension,
I need to check file extension when search over a directory. if using re
i want to check the type of uploaded file. If name like example.txt ,
So I have an upload script, and I want to check the file type
Possible Duplicate: How to check file types of uploaded files in PHP? I have
Im trying to do a MD5 check for a file uploaded to a varbinary
I have form for file uploading in my website that i check mime-type of
Checking for mime type in php is pretty easy but as far as I
I am using Uploadify with ASP.Net 4.0 and a Generic Handler for the backend.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.