鬼鬼DH

导航

 

<body>
    <form id="form1" runat="server">
    <div>
        <span style="color: #ff0000">
    目前首页上的图片:</span><br />
        <asp:Image ID="Image1" runat="server" Height="180px" Width="235px" />
          <asp:Image ID="Image2" runat="server" Height="180px" Width="235px" />
            <asp:Image ID="Image3" runat="server" Height="180px" Width="235px" />
              <asp:Image ID="Image4" runat="server" Height="180px" Width="235px" />
        <br />
        <br />
        <span style="color: #ff0000">选择替换需要显示在主页动态图片的新闻(注:最多只能选4项)</span><br />
        <table style="width: 374px;  border-color:Red;" border=1 cellpadding=0  cellspacing=0 >
            <tr>
                <td style="width: 100px">
                    <asp:FileUpload ID="FileUpload1" runat="server" /></td>
                <td style="width: 100px">
                    <asp:Button ID="Button1" runat="server" Text="替换图片一" OnClick="Button1_Click" /></td>
            </tr>
            <tr>
                <td style="width: 100px">
                    <asp:FileUpload ID="FileUpload2" runat="server" /></td>
                <td style="width: 100px">
                    <asp:Button ID="Button3" runat="server" Text="替换图片二" OnClick="Button3_Click" /></td>
            </tr>
            <tr>
                <td style="width: 100px">
                    <asp:FileUpload ID="FileUpload3" runat="server" /></td>
                <td style="width: 100px">
                    <asp:Button ID="Button4" runat="server" Text="替换图片三" OnClick="Button4_Click" /></td>
            </tr>
            <tr>
                <td style="width: 100px">
                    <asp:FileUpload ID="FileUpload4" runat="server" /></td>
                <td style="width: 100px">
                    <asp:Button ID="Button5" runat="server" Text="替换图片四"  OnClick="Button5_Click" /></td>
            </tr>
        </table>
        <br />
        &nbsp;</div>
    </form>
</body>

前台如图所示:

四张图片的文件名为pic1,pic2,pic3,pic4
我上传的图片需要改成其中一张, 思路是先通过上传到服务器这些图片所在的文件夹IMAGE中,然后再修改文件名。
修改文件名的思路是先COPY在删除源文件。
后台代码如下:先要引用SYSTEM.IO

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            imagebind();
           
        }

    }
    private void imagebind()
    {
        Image1.ImageUrl = "image/pic1.jpg";
        Image2.ImageUrl = "image/pic2.jpg";
        Image3.ImageUrl = "image/pic3.jpg";
        Image4.ImageUrl = "image/pic4.jpg";
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        tihuan(FileUpload1, "pic1.jpg");
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        tihuan(FileUpload2, "pic2.jpg");
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        tihuan(FileUpload3, "pic3.jpg");
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        tihuan(FileUpload4, "pic4.jpg");
    }
    private void tihuan(FileUpload myFileUpload,string newname)
    {
        bool b = Upload(myFileUpload);  //   
        if (!b)
        {
            return;
        }
        string OrignFile, NewFile;
        OrignFile = Server.MapPath(".") +  "\\image\\"+myFileUpload.FileName;
        NewFile = Server.MapPath(".") + "\\image\\"+newname;   
        File.Copy(OrignFile, NewFile, true);
        File.Delete(OrignFile);
    }
    private bool Upload(FileUpload myFileUpload)
    {
        bool flag = false;
        //是否允许上载  
        bool fileAllow = false;
        //设定允许上载的扩展文件名类型  
        string[] allowExtensions = { ".jpeg", ".gif", ".bmp", ".png", ".jpg", ".tiff" };

        //取得网站根目录路径  
        string path = HttpContext.Current.Request.MapPath("image/");
        //检查是否有文件案  
        if (myFileUpload.HasFile)
        {
            //myFileUpload.FileName = "pic1111111.jpg";
            //取得上传文件之扩展文件名,并转换成小写字母  
            string fileExtension = System.IO.Path.GetExtension(myFileUpload.FileName).ToLower();
            //检查扩展文件名是否符合限定类型  
            for (int i = 0; i < allowExtensions.Length; i++)
            {
                if (fileExtension == allowExtensions[i])
                {
                    fileAllow = true;
                }
            }

            if (fileAllow)
            {
                try
                {
                    //存储文件到文件夹  
                    myFileUpload.SaveAs(path + myFileUpload.FileName);
                    flag = true;
                }
                catch (Exception ex)
                {
                    PubClass.ShowMessageBox(Page, ex.Message);
                    flag = false;
                }
            }
            else
            {
                PubClass.ShowMessageBox(Page, "不允许上载:" + myFileUpload.PostedFile.FileName + ",只能上传图片文件,请检查!");

                flag = false;
            }
        }
        else
        {
            PubClass.ShowMessageBox(Page, "请选择要上传的图片文件!");
            flag = false;
        }
        return flag;
    }

最关键的语言:
    string OrignFile, NewFile;
        OrignFile = Server.MapPath(".") +  "\\image\\"+myFileUpload.FileName;
        NewFile = Server.MapPath(".") + "\\image\\"+newname;   
        File.Copy(OrignFile, NewFile, true);
        File.Delete(OrignFile);
代码很简单,想法也很清楚。
请高手给给更简单的方式实现文件上传并修改文件名。谢谢

posted on 2009-07-28 16:54  鬼鬼DH  阅读(2285)  评论(0编辑  收藏  举报