Single image upload--"select" + "Upload",. NET default mode:
1. Principle of implementation:With the default use of the FileUpload control, the Select button selects the picture, then clicks the Upload button to complete the upload, and loads the uploaded picture in the Click event of the Upload button.
2. Key code:
Page code:
1<asp:fileupload id="FileUpload"runat="Server"/>2<asp:button id="Btnup"runat="Server"onclick="Btnup_click"text="Upload"/>3<asp:label id="labmsg"runat="Server"></asp:Label>4"img"runat="Server"Src=""/>
Background code:
1 /// <summary>2 ///Upload a single image--There are "select" + "Upload" two buttons3 /// </summary>4 /// <param name= "Sender" ></param>5 /// <param name= "E" ></param>6 protected voidBtnup_click (Objectsender, EventArgs e)7 {8 if(Fileupload.hasfile)9 {Ten stringSavepath = Server.MapPath ("~/upload/");//specify the path to save the uploaded file on the server One //Check that the physical path exists on the server and create if it does not exist A if(!System.IO.Directory.Exists (savepath)) - { - System.IO.Directory.CreateDirectory (savepath); the } -Savepath = Savepath +"\\"+Fileupload.filename; - Fileupload.saveas (savepath); -Labmsg.text =string. Format ("<a href= ' upload/{0} ' >upload/{0}</a>", fileupload.filename); + This. img. SRC ="upload/"+Fileupload.filename; - } + Else A { atLabmsg.text ="you have not yet chosen to upload the file!"; - } -}
ASP. FileUpload control implements picture upload with preview display