| private void StartUpLoad() { if (FileUpload1.HasFile) { HttpPostedFile postedFile = FileUpload1.PostedFile; if (IsImageFile(postedFile)) { //Save image here } else { Response.Write("Invalid File, Cannot Upload!"); } } else { Response.Write("Please select a File"); } } protected bool IsImageFile(HttpPostedFile file) { bool isImage = false; System.IO.FileStream fs = new System.IO.FileStream(file.FileName, 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 { return false; } finally { br.Close(); fs.Close(); } /*extension lists with codes *7173 gif *255216 jpg *13780 png *6677 bmp *239187 txt,aspx,asp,sql *208207 xls.doc.ppt *6063 xml *6033 htm,html *4742 js *8075 xlsx,zip,pptx,mmap,zip *8297 rar *01 accdb,mdb *7790 exe,dll *64101 bat */ //only allow images jpg gif bmp png String[] fileType = { "255216", "7173", "6677", "13780" }; for (int i = 0; i < fileType.Length; i++) { if (fileclass == fileType[i]) { isImage = true; break; } } return isImage; } |