I want to validate a type of an uploaded file (suppose the file has incorrect extension).
I figure it could be done by checking the magic number of file signature. Similarly, as it is done in unix. So I guess I need to read the first bytes of the file and compare them with the magic number mappings (and I need to have the mappings and I need to maintain them).
So I thought maybe there is a class out there just for this purpose? Or maybe someone has done this and can share their insights开发者_StackOverflow社区 on the solution? Thanks
I doubt there's a “silver bullet” solution for this problem. Nobody says every file format has any form of a magic number. Consider plain text files — no reasonable indication at all (beside a possible byte-order-mark at the beginning which, however, need not mean anything) and pure heuristics must be used to guess it's a plain text file. Like wise to recognize an XML or HTML (i.e. SGML) file it means to check a lot of possibilities.
The approach you should follow is:
- Narrow down the set of expected file formats.
- Either do-it-yourself based on the knowledge of the overall structure and/or characteristics of the file formats you need to support or look for a solution like GNU File mentioned by rerun. You can also have a look on sites like MagicDB — however, this one is particularly outdated (year 2005).
- Specify behavior for unexpected file formats.
In case the set is narrow “enough” I'd consider a simple custom solution as the right one.
i don't know of a .net option but you can see if file for windows from gnu solves your problem.
精彩评论