/// <summary> /// 图片上传,按照限制的图片宽高剪切 /// </summary> /// <param name="fuUrl">上传控件</param> /// <param name="filePath">上传图片要存放的文件夹。格式为:/UploadImage/background/</param> /// <param name="limitWidth">上传图片最大宽度 0:表示不限制上传图片宽度</param> /// <param name="limitHeight">上传图片最大高度 0:表示不限制上传图片高度</param> /// <param name="msg">上传成功或失败的提示信息</param> /// <param name="imgName">上传成功后的文件名。若上传失败则为空</param> /// <returns>true:表示上传成功 false:上传失败</returns> public static bool UploadImage(FileUpload fuUrl, string filePath, int limitWidth, int limitHeight, out string msg, out string imgName) { if (fuUrl.PostedFile.FileName.IsNullOrEmpty()) { msg = "上传图片不能为空!"; imgName = ""; return(false); } string filepath = fuUrl.PostedFile.FileName; //得到的是文件的完整路径,包括文件名 string filename = filepath; //得到上传的文件名20022775_m.jpg string suffix = FileManager.GetFileExtension(filename); //获得上传的图片的后缀名 if (!allowPic(suffix)) { msg = "图片格式不正确,只能是jpg,gif,png格式"; imgName = ""; return(false); } //上传的图片改名改名 filename = Utility.GetRandomNumber() + suffix; string serverpath = HttpContext.Current.Server.MapPath(filePath + filename);//取得文件在服务器上保存的位置 using (System.Drawing.Image originalImage = System.Drawing.Image.FromStream(fuUrl.PostedFile.InputStream)) { //图片裁切 using (System.Drawing.Image customerImage = UploadFile.CutImg(originalImage, limitWidth, limitHeight, CutMode.WIDTH, true)) { //EventLog.WriteLog(customerImage.Width.ToString()); //图片保存 customerImage.Save(serverpath, System.Drawing.Imaging.ImageFormat.Jpeg); } } msg = "上传成功!"; imgName = filePath + filename; return(true); }
/// <summary> /// 图片上传 /// </summary> /// <param name="iswatermark">是否添加水印:0否 1是</param> /// <param name="fuUrl">上传控件</param> /// <param name="filePath">上传图片要存放的文件夹。格式为:/UploadImage/background/</param> /// <param name="msg">上传成功或失败的提示信息</param> /// <param name="imgName">上传成功后的文件名。若上传失败则为空</param> /// <returns>true:表示上传成功 false:上传失败</returns> public static bool UploadImage(int iswatermark, FileUpload fuUrl, string filePath, out string msg, out string imgName) { if (fuUrl.PostedFile.FileName.IsNullOrEmpty()) { msg = "上传文件不能为空!"; imgName = ""; return(false); } string filepath = fuUrl.PostedFile.FileName; //得到的是文件的完整路径,包括文件名 string filename = filepath; //得到上传的文件名20022775_m.jpg string suffix = FileManager.GetFileExtension(filename); //获得上传的图片的后缀名 if (!allowPic(suffix)) { msg = "图片格式不正确,只能是jpg,gif,png格式"; imgName = ""; return(false); } //上传的图片改名改名 filename = Utility.GetRandomNumber() + suffix; string serverpath = HttpContext.Current.Server.MapPath(defpath + filename); //取得文件在服务器上保存的位置 fuUrl.PostedFile.SaveAs(serverpath); //将上传的文件另存为 msg = "上传成功!"; imgName = filename; //添加图片水印 string imageOldPath = HttpContext.Current.Server.MapPath(filePath + filename); if (iswatermark == 1 && File.Exists(watermarkpic)) { AddShuiYinPic(serverpath, imageOldPath, watermarkpic); } return(true); }
private void Application_BeginRequest(Object source, EventArgs e) { HttpApplication Application = (HttpApplication)source; HttpResponse Response = Application.Context.Response; #region 判断域名是二级城市域名,如果是则重定向到相应目录 string oldUrl = Application.Context.Request.Url.ToString().ToLower(); //EventLog.WriteLog(oldUrl); //url中含有/city/sanmenxia,错误地址,不能直接访问,进行替换 //oldUrl = oldUrl.Replace("/city/sanmenxia", ""); //解决在分站访问当页面路径唯有文件名情况下。系统自动会加上/city/sanmenxia而导致的地址栏中实际显示的路径与访问的虚拟路径不同的问题 if (oldUrl.Contains("/city/sanmenxia")) { //直接重定向 Application.Context.Response.Redirect(oldUrl.Replace("/city/sanmenxia", "")); } if (UrlRewriteRules.OldCitySiteUrl.Contains("," + Application.Context.Request.Url.Host + ",")) { //EventLog.WriteLog(oldUrl); string newUrl = oldUrl.Replace("http://" + Application.Context.Request.Url.Host, UrlRewriteRules.NewCitySiteUrl); //解决访问的链接地址没有文件名的情况下,httpmodule会处理两次的情况 if (FileManager.GetFileExtension(newUrl).IsNullOrEmpty()) { if (!newUrl.EndsWith("/")) { newUrl += "/"; } newUrl += "index.aspx"; } //EventLog.WriteLog(newUrl); Application.Context.RewritePath(newUrl, false); return; } #endregion if (!FileManager.Exists(Application.Context.Request.PhysicalPath, FsoMethod.All)) { //EventLog.WriteLog(Application.Context.Request.PhysicalPath); //获取请求的URL string path = Application.Context.Request.RawUrl; //EventLog.WriteLog(path); //去除路径中?后面的内容 if (path.IndexOf("?") >= 0) { path = Utility.Left(path, path.IndexOf("?")); } //EventLog.WriteLog(path); //EventLog.WriteLog(path.IndexOf("?").ToString()); List <PathMapping> list = UrlRewriteRules.UrlRewrite(); //EventLog.WriteLog("开始1"); foreach (PathMapping pathMapping in list) { //EventLog.WriteLog("开始2"); string pattern = @"^" + pathMapping.VirtualPath + "$"; string realPathRules = pathMapping.RealPath; //匹配url规则 Regex regex = new Regex(pattern, RegexOptions.IgnoreCase); //EventLog.WriteLog(regex.IsMatch(path).ToString()); if (regex.IsMatch(path)) //如果匹配成功 { //获取匹配集合 Match mc = regex.Match(path); //EventLog.WriteLog("mc.Groups.Count:" + mc.Groups.Count + "<br/>"); string realPath = ""; for (int i = 0; i < mc.Groups.Count; i++) { //EventLog.WriteLog("continue前"); if (i == 0) //第一个匹配串是整串匹配,没用,丢弃 { continue; } //EventLog.WriteLog("continue后"); //从第二个匹配字串起,替换$1,$2,$3这样url串中的参数 realPath = realPathRules.Replace("$" + i.ToString(), mc.Groups[i].Value); //realPathRules值重置,因为如果是多个参数$1,$2,$3,不重置,则上行的realPathRules仍然是原来的$1,$2,$3,循环替换就没有成功 realPathRules = realPath; //EventLog.WriteLog(realPath); } //EventLog.WriteLog(realPath); //重写url Application.Context.RewritePath(realPath); return; } } } }
/// <summary> /// 图片上传,限制图片大小和宽高 /// </summary> /// <param name="fuUrl">上传控件</param> /// <param name="filePath">上传图片要存放的文件夹。格式为:/UploadImage/background/</param> /// <param name="limitSize">上传图片最大值以字节为单位 0:表示不限制上传图片大小</param> /// <param name="limitWidth">上传图片最大宽度 0:表示不限制上传图片宽度</param> /// <param name="limitHeight">上传图片最大高度 0:表示不限制上传图片高度</param> /// <param name="msg">上传成功或失败的提示信息</param> /// <param name="imgName">上传成功后的文件名。若上传失败则为空</param> /// <returns>true:表示上传成功 false:上传失败</returns> public static bool UploadImage(int iswatermark, FileUpload fuUrl, string filePath, int limitSize, int limitWidth, int limitHeight, out string msg, out string imgName) { if (fuUrl.PostedFile.FileName.IsNullOrEmpty()) { msg = "上传图片不能为空!"; imgName = ""; return(false); } if (limitSize > 0) { if (fuUrl.PostedFile.ContentLength > limitSize) { msg = "上传图片不能超过" + limitSize / 1024 + "M字节!"; imgName = ""; return(false); } } string filepath = fuUrl.PostedFile.FileName; //得到的是文件的完整路径,包括文件名 string filename = filepath; //得到上传的文件名20022775_m.jpg string suffix = FileManager.GetFileExtension(filename); //获得上传的图片的后缀名 if (!allowPic(suffix)) { msg = "图片格式不正确,只能是jpg,gif,png格式"; imgName = ""; return(false); } if (limitWidth > 0 || limitHeight > 0) //表示限制了宽高 { using (Stream stream = fuUrl.PostedFile.InputStream) { using (System.Drawing.Image img = System.Drawing.Image.FromStream(stream)) { if (limitWidth > 0 && img.Width > limitWidth) { msg = "上传图片宽度不能超过" + limitWidth + "px!"; imgName = ""; return(false); } if (limitHeight > 0 && img.Height > limitHeight) { msg = "上传图片高度不能超过" + limitHeight + "px!"; imgName = ""; return(false); } stream.Close(); } } } //上传的图片改名改名 filename = Utility.GetRandomNumber() + suffix; string serverpath = HttpContext.Current.Server.MapPath(filePath + filename); //取得文件在服务器上保存的位置 fuUrl.PostedFile.SaveAs(serverpath); //将上传的文件另存为 msg = "上传成功!"; imgName = filename;; //添加图片水印 string imageOldPath = HttpContext.Current.Server.MapPath(filePath + filename); //EventLog.WriteLog(serverpath); //EventLog.WriteLog(imageOldPath); if (iswatermark == 1 && File.Exists(watermarkpic)) { AddShuiYinPic(serverpath, imageOldPath, watermarkpic); //AddShuiYinWord(serverpath, imageOldPath); } return(true); }