/// <summary> /// 图片上传保存 /// </summary> /// <param name="files"></param> /// <returns></returns> public static UpLoadResult UploadImage(HttpPostedFileBase files) { UpLoadResult uploadResult = new UpLoadResult(); DateTime N = System.DateTime.Now; Random R = new Random(); if (files.ContentLength > 0) { HttpPostedFileBase PostFile = files; //新的文件名称 string AttachmentsName = N.Year.ToString() + N.Month.ToString() + N.Day.ToString() + N.Hour.ToString() + N.Minute.ToString() + N.Second.ToString() + R.Next(); //文件后缀 string FileExt = System.IO.Path.GetExtension(PostFile.FileName).ToLower(); if (("|.jpg|.png|.jepg|").IndexOf("|" + FileExt + "|") < 0) //判断上传附件是否是允许上传的附件 { uploadResult.file_path = ""; uploadResult.result = "请上传类型为.jpg|.png|.jepg|的图片"; } StringBuilder sb = new StringBuilder(); string filename = AttachmentsName; //从配置文件中获取图片保存的路径 var path = Configs.GetValue("ImgSrc"); if (!FileHelper.IsExistDirectory(path)) { FileHelper.CreateDir(path); } PostFile.SaveAs(path + filename + "." + FileExt); uploadResult.file_path = path + filename + "." + FileExt; uploadResult.result = "上传成功"; } else { uploadResult.file_path = ""; uploadResult.result = "没有选择要上传的文件"; } return(uploadResult); }
public static FileModel Upload(HttpPostedFileBase postedFile) { //上传和返回(保存到数据库中)的路径 string uppath = string.Empty; string savepath = string.Empty; HttpContext context = HttpContext.Current; //创建图片新的名称 string nameImg = DateTime.Now.ToString("yyyyMMddHHmmssfff"); string strPath = postedFile.FileName; string type = strPath.Substring(strPath.LastIndexOf(".") + 1).ToLower(); if (ValidateImg(postedFile)) { //拼写上传图片的路径 savepath = context.Server.MapPath(Configs.GetValue("FileSrc") + type + "/"); savepath += nameImg + "." + type; uppath = context.Server.MapPath(Configs.GetValue("FileSrc")) + type; FileHelper.CreateDirectory(uppath); FileHelper.CreateDirectory(uppath + "/水印"); //水印图片地址 FileHelper.CreateDirectory(uppath + "/文字"); //文字图片地址 //uppath += nameImg + "." + type; //上传图片 postedFile.SaveAs(savepath); ImageManager writer = new ImageManager(); //调用很简单 im.SaveWatermark(原图地址, 水印地址, 透明度, 水印位置, 边距,保存到的位置); writer.SaveWatermark(savepath, context.Server.MapPath(Configs.GetValue("FileSrc")) + "Logo.png", 0.9f, ImageManager.WatermarkPosition.RigthBottom, 10, uppath + "/水印/" + nameImg + "." + type); writer.AddTextToImg(savepath, "蓝色星球", uppath + "/文字/" + nameImg + "." + type); } return(new FileModel() { RealName = nameImg + "." + type, FileName = strPath, FileMax = postedFile.ContentLength.ToString() }); }
public static string SEOSiteName() { return(Configs.GetValue("SiteName")); }