Form submission file

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

 

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.