Document and Video file format extension verification and image real verification help class, file format truth

Source: Internet
Author: User

Document and Video file format extension verification and image real verification help class, file format truth

Using System; using System. collections. generic; using System. linq; using System. text; using System. IO; namespace Reform. folderManage. cls {public class FileHelper {/// <summary> /// image class header encoding /// </summary> public enum ImgExtension {JPG = 255216, GIF = 7173, BMP = 1, 6677, PNG = 13780} /// <summary> // file type determination // </summary> /// <param name = "fileName"> file </param> /// <param name = "fileType"> file type </param> /// <returns> </returns> public static bool IsRealFile (string fileName, string fileType) {string fileExt = Path. getExtension (fileName ). toLower (); // get the file suffix if (fileType = "image") {if (IsAllowedExtension (fileName) return true;} if (fileType = "video ") {List <string> list = new List <string> (); list. add (". flv "); list. add (". wmv "); list. add (". avi "); list. add (". 3gp "); Path. getExtension (fileName ). toLower (); // get the file suffix return IsExistEx (list, fileExt);} if (fileType = "document ") {List <string> list = new List <string> (); list. add (". doc "); list. add (". pdf "); return IsExistEx (list, fileExt);} return false ;} /// <summary> /// determine the file suffix /// </summary> private static bool IsExistEx (List <string> list, string fileExt) {if (list. contains (fileExt) // {return true;} return false;} in the list ;} # region image processing // <summary> /// whether the image type is true // </summary> public static bool IsAllowedExtension (string imgFileName) {System. IO. fileStream fs = new System. IO. fileStream (imgFileName, System. IO. fileMode. open, System. IO. fileAccess. read); System. IO. binaryReader br = new System. IO. binaryReader (fs); string fileclass = ""; byte buffer; try {buffer = br. readByte (); fileclass = buffer. toString (); buffer = br. readByte (); fileclass + = buffer. toString ();} catch {} br. close (); fs. close (); ImgExtension [] fileEx = {ImgExtension. BMP, ImgExtension. GIF, ImgExtension. JPG, ImgExtension. PNG}; foreach (ImgExtension fe in fileEx) {if (Int32.Parse (fileclass) = (int) fe) return true ;}return false ;}# endregion }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.