Checking Mime File

I allow files to be uploaded to my C # MVC website, I restrict these types based on the extension at the moment, but also feel like I need a server side check to confirm that they did not just rename it.

Is there a way that I can use to check all the types that I need, or a library that I can use that will help here?

I saw people checking the first few bytes of a file, but am I afraid that I will miss something?

Thank you for your help.

Edit:

Many suggestions are presented here. I will explore some of them as a solution.

+4
source share
3 answers

I ended up mixing some solutions from here , because I do not use HttpFileBase and only have a file stream, I had to read the first few bytes to determine the type of mime.

Note. I do not use the registry technique because I do not know what will or will not be installed on the servers.

0
source

If you read the file as an HttpPostedFile, you can get the content type equal to the mime type.

So you can do the following:

if (myFile.ContentType == "video/mpeg") { // Do your thing } else{ // error } 
+1
source

Try this solution: Using .NET, as you can find the mime file type based on the file name, not the extension

It will spoof the contents of the file for you.

0
source

All Articles