This article mainly introduces the use of ASP.net to determine the type of upload file three ways, the need for friends can refer to the following
First, the security is relatively low, the text file 1.txt to 1.jpg can be uploaded, but its implementation is easy to understand, implementation is also simple, so many online or take this approach. Code as follows: Boolean fileok = false; String path = Server.MapPath ("~/images/"); //Determine if file if (F Ileupload1.hasfile) { //Get file extension and convert to lowercase string fileextension = System.IO.Path.GetExtension ( Fileupload1.filename). ToLower (); //qualification can only upload jpg and gif pictures string[] allowextension = {". jpg", ". gif"}; //to the type of uploaded file Int J = 0; for (int i = 0; i < allowextension.length i++) { if (fileextension = Allowextension[i]) & nbsp { FileOK = true; return; } else { j++; } } if (J > 0) {&nbs P Response.Write ("<script>alert (' incorrect file format ');</script>"); return; } } else { Response.Write (' You have not selected file ');</script>; return; } //If the extension meets the criteria, Then upload if (fileok) { FileUpload1.PostedFile.SaveAs (path + FileuploaD1. FileName); Response.Write ("<script>alert (' uploaded success ');</script>"); } II, does not detect file suffixes but detects file MIME content types. Code is as follows: Boolean fileok = false; String path = Server.MapPath ("~/images/"); //Determine if the file if is selected (FILEUPL Oad1. HasFile) { //Get file MIME content type String type = this. FileUpload1.PostedFile.ContentType.ToLower (); if (type. Contains ("image")//the MIME type of the picture is "image/xxx", here only determines whether the picture. { FileOK = true; } else { ("Response.Write (' malformed ') <script>alert > "); } } else { Response.Write (" You have not selected file ') <script>alert "); } //If the extension meets the criteria, upload if (fileok) { FileUpload1.PostedFile.SaveAs (path + Fileupload1.filename); Response.Write ("<script>alert (' upload success ');</script>"); } Three , can realize the true sense of file type judgment code as follows: try { //Determine if the file has been selected if (fileupload1.hasfile) { if ( IsallowedexTension (FILEUPLOAD1)) { String path = Server.MapPath ("~/images/"); FileUpload1.PostedFile.SaveAs ( Path + fileupload1.filename); Response.Write ("<script>alert (' upload success ');</script>"); } else { Response.Write ("<script>alert (' You can only upload jpg or gif images ');</script>"; } else { Response.Write ("You have not selected file");</script>; } } catch ( Exception error) { Response.Write (error. ToString ()); } #endregion } //True to determine the key function of the file type public static bool Isallowedextension ( FileUpload hifile) { System.IO.FileStream fs = new System.IO.FileStream (hifile. Postedfile.filename, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.BinaryReader r = new System.IO.BinaryReader (FS); string fileclass = ""; //Here's the bit length to be specifically judged . byte buffer; try Buffer = R.readbyte (); fileclass = buffer. Tostring(); buffer = R.readbyte (); Fileclass + = buffer. ToString (); } catch { } r.close (); fs. Close (); if (Fileclass = = "255216" | | | fileclass = = "7173")//Description 255216 is jpg;7173 is gif;6677 is bmp,13780 is EXE, 8297 is rar { return true; } else { return false; } }