public ActionResult Upload() { ViewBag.success = false; if (!AppData.IsManagerLogin) { ViewBag.msg = "抱歉,您未登录后台或会话已过期!"; return(View()); } if (PrivilegeBLL.HasNotPrivilege(AppData.SessionUserID, 1601)) { ViewBag.msg = "您没有执行该操作的权限!"; return(View()); } HttpPostedFileBase pic = Request.Files.Count == 0 ? null : Request.Files[0]; int mediaID = string.IsNullOrEmpty(Request.Form["id"]) ? 0 : int.Parse(Request.Form["id"]); if (pic != null && !string.IsNullOrEmpty(pic.FileName)) { int type = string.IsNullOrEmpty(Request.Form["type"]) ? 0 : int.Parse(Request.Form["type"]); string ext = Path.GetExtension(pic.FileName); if (type == 0) { if (!Regex.IsMatch(ext, @"^\.(gif|jpg|jpeg|png)$", RegexOptions.IgnoreCase)) { ViewBag.msg = "上传的图片格式不合要求,请上传gif,png,jpg格式的图片"; return(View()); } } else if (type == 1) { if (!Regex.IsMatch(ext, @"^\.(mp3|wma)$", RegexOptions.IgnoreCase)) { ViewBag.msg = "上传的图片格式不合要求,请上传mp3,wma格式的文件"; return(View()); } } string dirPath = Config.MediaPath + @"\Media"; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } MediaBLL mediaBLL = new MediaBLL(); MediaObj mediaObj = mediaBLL.GetMedia(mediaID); if (mediaObj != null) { System.IO.File.Delete(Config.MediaPath + mediaObj.SavePath); } else { mediaObj = new MediaObj(); mediaObj.MediaID = mediaID; } string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff") + ext; string savePath = Path.Combine(dirPath, newFileName); mediaObj.SavePath = @"\Media\" + newFileName; mediaObj.Src = "/Media/" + newFileName; mediaObj.Description = Request.Form["desc"]; mediaObj.Url = Request.Form["url"]; mediaObj.Content = Request.Form["content"]; mediaBLL.SetMedia(mediaObj); pic.SaveAs(savePath); ViewBag.success = true; return(View()); } else if (mediaID != 0) { MediaBLL mediaBLL = new MediaBLL(); MediaObj mediaObj = mediaBLL.GetMedia(mediaID); mediaObj.Description = Request.Form["desc"]; mediaObj.Url = Request.Form["url"]; mediaObj.Content = Request.Form["content"]; mediaBLL.SetMedia(mediaObj); ViewBag.success = true; return(View()); } else { ViewBag.msg = "请您选择一张图片上传"; return(View()); } }