ASP. NET upload judgment type to prevent hackers from uploading illegal files

Source: Internet
Author: User

Function class for checking file types:
Using System;
Using System. Collections. Generic;
Using System. Web. UI. WebControls;
Using System. IO;
Namespace 0x001. Pack
{
Public enum FileExtension
{
. JPG = 255216,
GIF = 7173,
BMP = 1, 6677,
PNG = 1, 13780
// 255216 jpg;
// 7173 gif;
/// 6677 bmp,
// 13780 png;
// 7790 exe dll,
// 8297 rar
// Xml 6063
// Html 6033
// 239187 aspx
// 117115 cs
// Js 119105
/// 210187 txt
// 255254 SQL
}
Public class FileValidation
{
Static bool IsAllowedExtension () static bool IsAllowedExtension (FileUpload fu, FileExtension [] fileEx)
{
Int fileLen = fu. PostedFile. ContentLength;
Byte [] imgArray = new byte [fileLen];
Fu. PostedFile. InputStream. Read (imgArray, 0, fileLen );
MemoryStream MS = new MemoryStream (imgArray );
System. IO. BinaryReader br = new System. IO. BinaryReader (MS );
String fileclass = "";
Byte buffer;
Try
{
Buffer = br. ReadByte ();
Fileclass = buffer. ToString ();
Buffer = br. ReadByte ();
Fileclass + = buffer. ToString ();
}
Catch
{
}
Br. Close ();
Ms. Close ();
Foreach (FileExtension fe in fileEx)
{
If (Int32.Parse (fileclass) = (int) fe)
Return true;
}
Return false;
& Nbs

Related Article

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.