Check the Server File Type in ASP. NET

Source: Internet
Author: User
Public static bool isallowextensin (system. Web. UI. htmlcontrols. htmlinputfile hifile) // check the file type
{
System. io. filestream FS = new system. io. filestream (hifile. postedfile. filename, system. io. filemode. open, system. io. fileaccess. read); // create a file stream object
System. Io. binaryreader r = new system. Io. binaryreader (FS); // create a file reader object
String fileclass = ""; // read two bytes from the current file stream to determine the file type
Byte buffer;
Try
{
Buffer = R. readbyte (); // read the next byte from the current stream
Fileclass = buffer. tostring ();
Buffer = R. readbyte ();
Fileclass + = buffer. tostring ();
}
Catch {}
R. Close (); // close the reader object
FS. Close (); // close the file stream
If (fileclass = "255216" | fileclass = "7173" | fileclass = "6677" | fileclass = "13780") // The 255216 is JPG; 7173 is GIF, 6677 is BMP, 13780 is PNG, 7790 is exe, and 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.