asp.net three effective ways to determine the type of uploaded files _ practical Tips

Source: Internet
Author: User
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;
}
}

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.