<asp:FileUploadID="FileUpload1" runat="server" /> <asp:ButtonID="Button1" runat="server" onclick="Button1_Click"Text="上传”/> <asp:ImageID="Image1" runat="server" Height="136px"Width="188px" /> |
protectedvoid Button1_Click(object sender,EventArgs e) { if(FileUpload1.HasFile)//判断是否选择文件 { string filename = FileUpload1.FileName;//获得上传文件名 if(filename.EndsWith(".jpg")||filename.EndsWith("docx"))//限制上传格式 { string strpath=Server.MapPath("images/"+filename);//把相对路径改成绝对路径 FileUpload1.SaveAs(strpath);//把文件传到服务器必须用绝对路径 Image1.ImageUrl = "images/" + filename;//显示用相对路径 } else { Page.ClientScript.RegisterStartupScript(this.GetType(),"","alert('格式不对')",true); } } } |