Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi , I have some files ( they should be Excel 2003/2007/...) and I wanted to check if they are really Excel files without dangerous scripts and so on , how can I do it in C# ?

see this plz : http://forum.sources.ru/mime.php[^]

in this page even if u change a file extention it can find the real mime type
Posted
Comments
[no name] 16-Jul-15 9:06am    
Use FileuploadId.PostedFile.ContentType;
Sergey Alexandrovich Kryukov 16-Jul-15 10:37am    
Nothing guarantees that this content type is correct. The question is exactly about that.
—SA
Sergey Alexandrovich Kryukov 16-Jul-15 10:36am    
Write non-dangerous scripts.
—SA

You cannot do it on the client side, because you cannot assume the client-side system has any of required software, even if that client sent an Excel file. And on server side it won't be a big problem.

Strictly speaking, you cannot know what is the file exactly, until you try to parse it all. Please see my comment to Solution 1. You can unzip .xlxs and see what inside. Learn what it should be there and do some check up. Or you can use Microsoft Open XML SDK and try to read the file directly via the SDK.

Please see my past answers:
Extract file rar with vb.net[^],
How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^].

—SA
 
Share this answer
 
The type of a file is determined by it's "magic number" which is a series of bytes that it places at the start of the file. To work out if it is Excel, jpg etc you need to know that file format's magic number, then check the bytes at the start of the file you have and see if they match

http://www.garykessler.net/library/file_sigs.html[^]

You can read the bytes by turning your file into byte[]. If you google "read file magic number c#" I'm sure you'll find some examples.
 
Share this answer
 
v2
Take a look:

Quote:
One way would be to check for certain signatures or magic numbers in the files. This page has a handy list of known file signatures and seems quite up to date:


FILE SIGNATURES TABLE[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Jul-15 10:39am    
Excel file (Open XML, not obsolete formats) is a bunch of of files zipped together. So, you would need to unzip it and check up what's inside.
See also Solution 2.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900