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.
Copy Code code as follows:
Boolean FileOK = false;
String path = Server.MapPath ("~/images/");
Determine if a file has been selected
if (fileupload1.hasfile)
{
Gets the file's extension and turns to lowercase
String fileextension = System.IO.Path.GetExtension (fileupload1.filename). ToLower ();
Restrict uploading jpg and GIF images only
String[] allowextension = {". jpg", ". gif"};
Make a pair of the types of uploaded files
int j = 0;
for (int i = 0; i < allowextension.length; i++)
{
if (fileextension = = Allowextension[i])
{
FileOK = true;
Return
}
Else
{
j + +;
}
}
if (J > 0)
{
Response.Write ("<script>alert (' incorrect file format ');</script>");
Return
}
}
Else
{
Response.Write ("You have not selected a file");</script> "<script>alert");
Return
}
If the extension meets the criteria, upload the
if (FileOK)
{
FileUpload1.PostedFile.SaveAs (path + fileupload1.filename);
Response.Write ("<script>alert (' upload success ');</script>");
}
Second, do not detect the file suffix, but detect the file MIME content type.
Copy Code code as follows:
Boolean FileOK = false;
String path = Server.MapPath ("~/images/");
Determine if a file has been selected
if (fileupload1.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 ("<script>alert (' malformed ') </script>");
}
}
Else
{
Response.Write ("You have not selected a file");</script> "<script>alert");
}
If the extension meets the criteria, upload the
if (FileOK)
{
FileUpload1.PostedFile.SaveAs (path + fileupload1.filename);
Response.Write ("<script>alert (' upload success ');</script>");
}
Third, can realize the real sense of document type judgment
Copy Code code as follows:
Try
{
Determine if a 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 pictures ');</script>");
}
}
Else
{
Response.Write ("You have not selected a file");</script> "<script>alert");
}
}
catch (Exception error)
{
Response.Write (Error. ToString ());
}
#endregion
}
Key functions to truly determine file types
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 = "";
The bit length here is to be judged specifically.
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 png;7790 is exe,8297 is rar
{
return true;
}
Else
{
return false;
}
}