Exemplo n.º 1
0
        public ActionResult changeHeadImg(HttpPostedFileBase file)
        {
            if (file == null)
            {
                return(RedirectToAction("Index", "Manage", new { headImgState = 0 }));
            }
            else
            {
                //根据用户名创建文件夹保存照片
                string user_id = ((User)Session["user"]).User_Id;
                string path    = Request.MapPath("~/Content/headimg/");
                string hz      = file.FileName.Substring(file.FileName.LastIndexOf("."));

                if (Directory.Exists(path) == false)//如果不存在就创建file文件夹
                {
                    Directory.CreateDirectory(path);
                }
                string fname = user_id + hz;
                file.SaveAs(path + fname);//保存文件
                //更新数据库
                User u = new User();
                u.User_Id = user_id;
                UserSql us = new UserSql();
                u = us.getUserInfo(u);
                u.User_HeadImg = "/Content/headimg/" + fname;
                us.changeHeadImg(u);
                //更新数据库

                return(RedirectToAction("Index", "Manage", new { headImgState = 1 }));
            }
        }