示例#1
0
        public ActionResult Edit(ResponseImage ResponseImage)
        {
            ResponseImage.UserId = WebSecurity.GetUserId(User.Identity.Name);
            ResponseImage.AddTime = DateTime.Now;
            if (ModelState.IsValid)
            {
                ResponseImageRepository.Update(ResponseImage);
                ResponseImageRepository.Context.Commit();

                return Redirect("/ResponseImage/Index");
            }
            return View(ResponseImage);
        }
示例#2
0
        public ActionResult UpLoadImage()
        {
            HttpPostedFileBase imgFile = Request.Files[0];
            if (imgFile != null)
            {
                if (User.Identity.Name != "")
                {
                    //获得上传图片的名字
                    string strPath = imgFile.FileName;
                    string type = strPath.Substring(strPath.LastIndexOf(".") + 1).ToLower();

                    //获取上传用户id
                    int userid = WebSecurity.GetUserId(User.Identity.Name);
                    ResponseImage ResponseImage = new ResponseImage();
                    ResponseImage.AddTime = DateTime.Now;
                    ResponseImage.UserId = userid;
                    ResponseImage.ImageName = imgFile.FileName.Replace("." + type, "");
                    string uppath = System.Web.HttpContext.Current.Server.MapPath("~/images/ResponseImage/" + User.Identity.Name.ToString());
                    ResponseImage.ImageUrl = "/images/ResponseImage/" + User.Identity.Name.ToString() + "/" + imgFile.FileName;
                    if (!Directory.Exists(uppath))
                    {
                        Directory.CreateDirectory(uppath);
                    }
                    if (ValidateImg(type))
                    {
                        if (imgFile.ContentLength < 1024 * 1024 * 2)
                        {
                            if (ResponseImageRepository.Find(Specification<ResponseImage>.Eval(o => o.ImageName == strPath.Replace("." + type, "") && o.UserId == userid)) == null)
                            {
                                imgFile.SaveAs(uppath + "\\" + imgFile.FileName);
                                if (ModelState.IsValid)
                                {
                                    ResponseImageRepository.Add(ResponseImage);
                                    ResponseImageRepository.Context.Commit();
                                    return Redirect("/ResponseImage/Index");
                                }
                            }
                            else
                            {
                                TempData["ErrorMessage"] = "图片名称已经存在";
                            }
                        }
                        else
                        {
                            TempData["ErrorMessage"] = "图片大小小于2M";
                        }
                    }
                    else
                    {
                        TempData["ErrorMessage"] = "图片内容不符合格式";
                    }
                }
                else
                {
                }
            }
            else
            {
                TempData["ErrorMessage"] = "上传图片不能为空";
            }
            return Redirect("/ResponseImage/Create");
        }