I used to send. net Method for uploading files, but the method for determining the file type is only to determine the suffix, so if I change the suffix of a TXT text file to JPG, you can also upload, this inadvertently causes security problems.
I just found a method from the Internet and tried it to identify the correct file type, as shown below:
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. web; <br/> using system. web. ui; <br/> using system. web. UI. webcontrols; <br/> using system. io; </P> <p> Public partial class niunantest: system. web. UI. page <br/>{< br/> protected void page_load (Object sender, eventargs E) <br/>{</P> <p >}< br/> protected void button#click (Object sender, eventargs e) <br/> {< Br/> string STR = fileupload1.postedfile. contenttype; <br/> response. write ("file type:" + Str); </P> <p> string filename = ""; </P> <p> fileextension [] Fe = {fileextension. GIF, fileextension. JPG, fileextension. PNG };< br/> If (filevalidation. isallowedextension (fileupload1, Fe) <br/>{< br/> string fileext = system. io. path. getextension (fileupload1.filename ). tolower (); <br/> response. write ("<br> Verification passed! "); <Br/> // filename ="/images/"+ datetime. now. tostring ("yyyymmddhhmmss") + fileext; <br/> // fileupload1.postedfile. saveas (server. mappath (filename); <br/>}< br/> else <br/>{< br/> response. write ("<br> verification fails. Only images in the following formats are supported: JPG, GIF, and PNG"); <br/> return; <br/>}</P> <p> Public Enum fileextension <br/> {<br/> JPG = 255216, <br/> GIF = 7173, <br/> PNG = 13780, <br/> SWF = 6787, <br/> RAR = 8297, <br/> zip = 8075, <br/> _ 7z = 55122 </P> <p> // 255216 JPG; </P> <p> // 7173 GIF; </P> <p> // 6677 BMP, </P> <p> // 13780 PNG; </P> <p> // 6787 SWF </P> <p> // 7790 exe dll, </P> <p> // 8297 RAR </P> <p> // 8075 zip </P> <p> // 55122 7z </P> <p> // 6063 XML </P> <p> // 6033 HTML </P> <p> // 239187 aspx </P> <p> // 117115 CS </P> <p> // 119105 JS </P> <p> // 102100 TXT </P> <p> // 255254 SQL </P> <p >}</ p> <p> public class filevalidation <br/> {<br/> Public static bool isallowedextension (fileupload Fu, fileextension [] fileex) <br/>{< br/> int filelen = Fu. postedfile. contentlength; <br/> byte [] imgarray = new byte [filelen]; <br/> Fu. postedfile. inputstream. read (imgarray, 0, filelen); <br/> memorystream MS = new memorystream (imgarray); <br/> system. io. binaryreader BR = new system. io. binaryreader (MS); <br/> string fileclass = ""; <br/> byte buffer; <br/> try <br/>{< br/> buffer = BR. readbyte (); <br/> fileclass = buffer. tostring (); <br/> buffer = BR. readbyte (); <br/> fileclass + = buffer. tostring (); <br/>}< br/> catch <br/>{< br/>}< br/> Br. close (); <br/> MS. close (); <br/> foreach (fileextension Fe in fileex) <br/>{< br/> If (int32.parse (fileclass) == (INT) Fe) <br/> return true; <br/>}< br/> return false; <br/>}< br/>
Personal Understanding: In the code above, the file type should be converted to binary bytes, and then the first two bytes are taken, in this case, the first two bytes indicate the file type...