Code
< Html Xmlns = "Http://www.w3.org/1999/xhtml" >
< Head Runat = "Server" >
< Title > No title page </ Title >
< Script Type = "Text/JavaScript" >
Function Upfile (){
VaR Form = Document. dform;
// Check the filename first
VaR File = Document. getelementbyid ( " Ifile " ). Value;
VaR Pos = File. lastindexof ( ' . ' );
VaR EXT = File. substring (Pos + 1 ). Tolowercase ();
If (Ext ! = ' JPG ' && EXT ! = ' GIF ' && EXT ! = ' PNG ' )
{
Alert ( ' Error: You must upload a JPG, GIF, or PNG file! ' );
Return ;
}
// Replace form options
VaR Action = Form. Action;
VaR Enctype = Form. enctype;
VaR Target = Form.tar get;
Form. Action = " Default3.aspx " ;
Form. enctype = ' Multipart/form-Data ' ;
If ( Typeof Form. Encoding ! = ' Undefined ' ) Form. Encoding = ' Multipart/form-Data ' ;
Form. Submit ();
}
</ Script >
</ Head >
< Body >
< Form ID = "Dform" Name = "Dform" Method = "Post" Runat = "Server" >
< Div >
< Input ID = "Ifile" Name = "Ifile" Type = "File" Onchange = "Upfile ()" />
</ Div >
</ Form >
</ Body >
</ Html >
Code
Protected Void Page_load ( Object Sender, eventargs E)
{
If (Request. Files. Count > 0 )
{
String Path = Server. mappath ( " Uploadfile/ " );
Httppostedfile postfile = Request. Files [ 0 ];
If (Postfile. contentlength > 0 )
{
String Name = Datetime. Now. tostring ( " Yymmddhhmmssfff " ) + Postfile. filename;
Postfile. saveas (Path + Name );
Page. clientscript. registerstartupscript ( This . GetType (), " JS " , " <SCRIPT> printimage ('uploadfile/ " + Name + " ') </SCRIPT> " );
}
}
}