How do i verify a file format?
Basically this question except i am using ASP.NET. How do i take a given file and check if it is
- image (all formats except bitmap including photoshop)
- archive (7z, rar, zip, etc). NOTE: I know a file can be both an image and zip. I can check if its both in code.
- sound (most such as m4a, flac, ogg, but开发者_StackOverflow中文版 not wav)
- model (3ds, X, B3D, etc)
The solution does not need to check all of these but if i can get a few to check all of these then great.
You can try to either:
- Find documentation for the file format and check for known bytes in the file header, etc
- Attempt to load the file using an API - if the file loads successfully then you know it is of the proper type.
For example, here is an example of how to verify an image file: validate-image-from-file-in-c#
Of course, the mime type and/or file extension can give you a good guess of what file format you are dealing with, but (depending on the source) you should not rely on it exclusively.
Given the variety of your file types, checking the extension may be the best option. Anyone has other thoughts?
精彩评论