用于上传文件,实现把本地的文件,传到服务器中去的 FileUpload控件

来源:互联网 发布:乐购数码淘宝是正品吗 编辑:程序博客网 时间:2024/05/04 09:34

<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);

            }

     }

}