public ActionResult AddPhoto() { if (Request.HttpMethod == "POST") { if (!AppData.IsManagerLogin) { return(HandleResult(false, "您未登录后台或会话已过期")); } if (PrivilegeBLL.HasNotPrivilege(AppData.SessionUserID, 1001)) { return(HandleResult(false, "您没有执行该操作的权限")); } Validation vld = new Validation(); PhotoObj photoObj = new PhotoObj(); photoObj.Name = vld.Get("name", false, "请填写照片名称"); photoObj.Info = vld.Get("info"); string sCategoryIDs = vld.Get("categoryIDs", false, "请至少选择一个类别", regex: @"^\d+(,\d+)*$", regexText: "类别参数错误"); IList <int> categoryIDs; if (string.IsNullOrEmpty(sCategoryIDs)) { categoryIDs = null; } else { string[] aCategoryIDs = sCategoryIDs.Split(','); categoryIDs = new List <int>(); for (int i = 0; i < aCategoryIDs.Length; i++) { categoryIDs.Add(int.Parse(aCategoryIDs[i])); } } HttpPostedFileBase pic = Request.Files.Count == 0 ? null : Request.Files[0]; if (pic == null || pic.ContentLength == 0) { return(HandleResult(false, "请选择一张照片")); } if (vld.HasError) { return(HandleResult(false, vld.GetError())); } photoObj.Categories = categoryIDs; string ext = Path.GetExtension(pic.FileName); if (!Regex.IsMatch(ext, @"^\.(gif|jpg|jpeg|png)$", RegexOptions.IgnoreCase)) { return(HandleResult(false, "上传的图片格式不合要求,请上传gif,png,jpg格式的图片")); } string path = DateTime.Now.ToString("yyyyMMdd"); string dirPath = Config.MediaPath + @"\Package\" + path; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff") + ext; string savePath = Path.Combine(dirPath, newFileName); pic.SaveAs(savePath); photoObj.Pic = "/Package/" + path + "/" + newFileName; PhotoBLL photoBLL = new PhotoBLL(); photoBLL.AddPhoto(photoObj); return(HandleResult(true, photoObj.PhotoID.ToString())); } else { if (!AppData.IsManagerLogin) { return(Redirect("/Manage/Error/1.html")); } if (PrivilegeBLL.HasNotPrivilege(AppData.SessionUserID, 1001)) { return(Redirect("/Manage/Error/2.html")); } return(View()); } }