/// <summary> /// http://ddddd.com/Common/File?id=1&FileName=./image/dddd&ImgType=1 /// </summary> /// <param name="Id"></param> /// <param name="FileName"></param> /// <param name="ImgType"></param> /// <returns></returns> public ActionResult CommonFile <T1>(int?Id, string FileName, string ImgType, bool watermark = true) where T1 : EntityBase <int> { // 修正从DB取得的图片与URL图片不符问题 if (!Id.HasValue && string.IsNullOrEmpty(FileName)) { LogManager.GetLogger(GetType()) .Error("Id and Filename are all empty! now returning default LillyLogoRed.png image!"); return(File(cmsHost + "Content/img/LillyLogoRed.png", "image/jpeg")); } //if (Id.HasValue) //{ string strPath = Server.MapPath("/" + FileName); LogManager.GetLogger(GetType()).Debug("Common.File, strPath=" + strPath); if (!System.IO.File.Exists(strPath)) { LogManager.GetLogger(GetType()).Debug("File not exists."); BaseService <T1> service = new BaseService <T1>("CAAdmin"); dynamic obj = service.Repository.GetByKey(Id.Value); if (obj != null) { if (string.IsNullOrEmpty(obj.ImageName)) { LogManager.GetLogger(GetType()) .Error("The image path in Database was empty!"); return(File(cmsHost + "Content/img/LillyLogoRed.png", "image/jpeg")); } // 正确获取数据库上图片信息 strPath = Server.MapPath("/" + obj.ImageName); if (!System.IO.File.Exists(strPath)) { CheckAndCreateDirectory(strPath); using (FileStream fs = new FileStream(strPath, FileMode.Create)) { fs.Write(obj.ImageContent, 0, obj.ImageContent.Length); fs.Close(); } } } else { return(File(cmsHost + "Content/img/LillyLogoRed.png", "image/jpeg")); } } if (!watermark) { return(File(strPath, "image/jpeg")); } Image wmImage; Image img = new Bitmap(strPath); try { // TODO: 先尝试实时添加水印的效果 if (User.Identity != null && !string.IsNullOrEmpty(User.Identity.Name)) { LogManager.GetLogger(GetType()).Debug("User.Identity found: " + User.Identity.Name); wmImage = ImageUtility.AddWatermarkText(img, User.Identity.Name, ImageAlign.RightBottom); return(File(ImageHelper.ImageToBytes(wmImage), "image/jpeg")); } else { // 否则暂时加个固定水印吧 wmImage = ImageUtility.AddWatermarkText(img, "仅供内部参考", ImageAlign.RightBottom); LogManager.GetLogger(GetType()) .Debug("User Identity not found, returning strPath + Internal Only:" + strPath); return(File(ImageHelper.ImageToBytes(wmImage), "image/jpeg")); } } catch (Exception ex) { LogManager.GetLogger(GetType()) .Debug("Exception in adding water mark, returning :" + cmsHost + "Content/img/LillyLogoRed.png" + "\t\r\nException: " + ex.Message + "\t\r\n" + ex.StackTrace); return(File(cmsHost + "Content/img/LillyLogoRed.png", "image/jpeg")); } }