string Sj = DateTime.Now.ToString("yyyyMMddHHmmss");//当前时间
Random ran = new Random(); string sjs = ran.Next(1000, 9999).ToString();//随机数 string pictureName = FileUpload1.FileName; string fileFix = pictureName.Remove(0, pictureName.LastIndexOf(".") + 1).ToLower();//图片后缀名 string fileName = Sj + sjs + "." + fileFix;
string ImgPath = "../companyImages/" + fileName; //2、修改图片时将原来文件夹的图片删除,添加新图片
//获取新图片名
DataSet ds = cf.GetCompanyImageById(userId); if (ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0]["PhotoName"].ToString() != "")//有图片时删除 { string OldImgPath = ds.Tables[0].Rows[0]["PhotoName"].ToString(); string OldServerPath = Server.MapPath(OldImgPath);//旧图片物理路径 //执行删除,将图片从旧文件夹中删除
System.IO.File.Delete(OldServerPath); } //将图片保存到相应的位置 string SavePath = Server.MapPath("../companyImages/") + fileName;
FileUpload1.PostedFile.SaveAs(SavePath);//将图片保存到相应的位置 |
|
来自: 万子千秋 > 《Asp.Net知识点》