标题:求代码,upload控件上传时改名
只看楼主
残影留香
Rank: 1
来 自:广西南宁
等 级:新手上路
帖 子:193
专家分:0
注 册:2008-3-19
得分:0 
应该是这样!
string fullname = this.File1.PostedFile.FileName;//取出上传文件的全部路径
        string filename = fullname.Substring(fullname.LastIndexOf("\\")+1);//截取最后一层目录下的文件名
        filename = System.DateTime.Now.ToString().Replace("-", "").Replace(" ", "").Replace(":", "");//给文件以当前日期时间重命名文件
        string type = fullname.Substring(fullname.LastIndexOf(".") + 1);//截取文件的后缀名
        filename = System.DateTime.Now.ToString().Replace("-", "").Replace(" ", "").Replace(":", "") +"."+ type;
        if (type == "jpg" || type == "gif" || type == "bmp")//判断上传文件的格式
        {
            this.File1.PostedFile.SaveAs(Server.MapPath("uploads") + "\\" + filename);
            this.Image1.ImageUrl = "uploads/" + filename;
            this.TextBox1.Text = "uploads/"+filename;
        }
        else
        {
            Response.Write("<script language=javascript>alert('你选择的图片格式不正确!');</script>");
        }
2008-03-31 10:46



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-128308-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.865726 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved