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

  • SEARCH
  • Home
  • 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 6123713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:02:56+00:00 2026-05-23T16:02:56+00:00

What is the best way to validate the file format in file-upload control in

  • 0

What is the best way to validate the file format in file-upload control in ASP.NET?
Actually I want that user only upload files with specific format. Although I validate it by checking file name but I am looking for another solution to over come this.

  • 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-05-23T16:02:57+00:00Added an answer on May 23, 2026 at 4:02 pm

    Try the following code which reads the first 256 bytes from the file and return the mime type of the file using an internal dll (urlmon.dll).Then compare the mime type of your file and the returned mime type after parsing.

         using System.Runtime.InteropServices; ...
    
               [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))
                {
                  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";
         }
        }
    

    But check the type in different browsers, since the mimetype might be different in different browsers.

    Also this will give the exact mimetype even if you changed the extension by editing the name of the file.

    Hope this helps you…

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

Sidebar

Related Questions

Which is best way to upload single file with asp.net mvc3 razor and validate
What's the best way to validate that an MAC address entered by the user?
What's the best way to validate an XML file (or a portion of it)
I need to validate that a text file is in CSV format (i.e. that
What is the best way to validate whether a .txt file is: In fact
I was wondering, what would be the best way to validate an integer. I'd
Which is the best way to implement factory method. 1) A factory method that
what is the best way to validate JSON data in Django/python. Is it best
What's the best way to validate controls, client-side JavaScript validation or server-side validation?
What is the best way to validate whether a NSString is a propertyList or

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.