System. Web. UI. htmlcontrols;
Public Partial ClassTest_truefile: system. Web. UI. Page
{
Protected VoidPage_load (ObjectSender, eventargs E)
{
}
/// <Summary>
/// C # functions for detecting real file types
/// </Summary>
/// <Param name = "hifile"> </param>
/// <Returns> </returns>
Private Bool Isallowedextension (httppostedfile hifile)
{
Bool RET = False ;
System. Io. filestream FS = New System. Io. filestream (hifile. filename, system. Io. filemode. Open, system. Io. fileaccess. Read );
System. Io. binaryreader R = New System. Io. binaryreader (FS );
String Fileclass = "" ;
Byte Buffer;
Try
{
Buffer = R. readbyte ();
Fileclass = Buffer. tostring ();
Buffer = R. readbyte ();
Fileclass + = Buffer. tostring ();
}
Catch
{
Return False ;
}
R. Close ();
FS. Close ();
/* File Extension description
* 4946/104116 txt
* 7173 GIF
* 255216 JPG
* 13780 PNG
* 6677 BMP
* 239187 txt, aspx, ASP, SQL
* 208207 xls.doc.ppt
* 6063 XML
* 6033 htm, html
* 4742 JS
* 8075 XLSX, zip, pptx, MMAP, zip
* 8297 RAR
* 01 accdb, MDB
* 7790 EXE, DLL
* 5666 PSD
* 255254 RDP
* 10056 BT seeds
* 64101 bat
* 4059 sgf
*/
//String [] filetype = {"255216", "7173", "6677", "13780", "8297", "5549", "870", "87111 ", "8075 "};
// Pure Image
String [] filetype = {
" 7173 " , // GIF
" 255216 " , // JPG
" 13780 " // PNG
};
For ( Int I = 0 ; I < Filetype. length; I ++ )
{
If (Fileclass = Filetype [I])
{
RET = True ;
Break ;
}
}
Response. Write (fileclass ); // You can output the file type Extension you do not know here.
Return RET;
}
Protected Void Btnok_click ( Object Sender, eventargs E)
{
If (Isallowedextension (uploadfile. postedfile ))
{
Response. Write ( " OK " );
}
}
}