using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO; using System.Net; using System.Text; using Itlead.Architecture.WebAppUI.Common; namespace Itlead.Architecture.WebAppUI.tools { /// /// jcFile 的摘要说明 /// public class jcFile : IHttpHandler { public void ProcessRequest(HttpContext context) { //context.Response.ContentType = "text/plain"; context.Response.Expires = 0; context.Response.CacheControl = "no-cache"; context.Response.AddHeader("Pragma", "No-Cache"); string _fileNamePath = ""; try { _fileNamePath = context.Request.Files[0].FileName; //开始上传 UpLoadImage(_fileNamePath, context); } catch { context.Response.Write("{msg: 0, msgbox: \"上传过程中发生意外错误!\"}"); } } public void UpLoadImage(string fileNamePath, HttpContext context) { try { string serverPath = System.Web.HttpContext.Current.Server.MapPath("~"); string toFilePath = Path.Combine(serverPath, "Upload/"); //获取要保存的文件信息 FileInfo file = new FileInfo(fileNamePath); //获得文件扩展名 string fileNameExt = file.Extension; //验证合法的文件 if (CheckImageExt(fileNameExt)) { //生成将要保存的随机文件名 string fileName = GetImageName() + fileNameExt; //获得要保存的文件路径 string NewFilePaht = DateTime.Now.ToString("yyyyMM") + "/" + DateTime.Now.ToString("dd"); string serverFileName = toFilePath + NewFilePaht; //物理完整路径 if (!Directory.Exists(serverFileName)) { Directory.CreateDirectory(serverFileName); } string toFileFullPath = serverFileName + "/" + fileName; //HttpContext.Current.Server.MapPath(toFilePath); //将要保存的完整文件名 string toFile = toFileFullPath;//+ fileName; ///创建WebClient实例 WebClient myWebClient = new WebClient(); //设定windows网络安全认证 方法1 myWebClient.Credentials = CredentialCache.DefaultCredentials; ////设定windows网络安全认证 方法2 context.Request.Files[0].SaveAs(toFile); //上传成功后网站内源图片相对路径 string relativePath = System.Web.HttpContext.Current.Request.ApplicationPath + string.Format(@"Upload/" + NewFilePaht + "/{0}", fileName); /* 比例处理 微缩图高度(DefaultHeight属性值为 400) */ System.Drawing.Image img = System.Drawing.Image.FromFile(toFile); int width = img.Width; int height = img.Height; float ratio = (float)width / height; //微缩图高度和宽度 int newHeight = height <= DefaultHeight ? height : DefaultHeight; int newWidth = height <= DefaultHeight ? width : Convert.ToInt32(DefaultHeight * ratio); FileInfo generatedfile = new FileInfo(toFile); string newFileName = "Thumb_" + generatedfile.Name; string newFilePath = Path.Combine(generatedfile.DirectoryName, newFileName); PictureHandler.CreateThumbnailPicture(toFile, newFilePath, newWidth, newHeight); string thumbRelativePath = System.Web.HttpContext.Current.Request.ApplicationPath + string.Format(@"Upload/" + NewFilePaht + "/{0}", newFileName); //返回原图和微缩图的网站相对路径 context.Response.Write("{msg:1, imgUrl: \"" + relativePath + "\",Thumb:\"" + thumbRelativePath + "\"}"); } else { // return "文件格式非法,请上传gif或jpg格式的文件。"; //throw new Exception("文件格式非法,请上传gif或jpg格式的文件。"); context.Response.Write("{msg: 0, msgbox: \"文件格式非法!\"}"); } } catch { // return ex.Message; context.Response.Write("{msg: 0, msgbox: \"上传过程中发生意外错误!\"}"); } } #region Private Methods /// /// 检查是否为合法的上传图片 /// /// /// private bool CheckImageExt(string imageExt) { string[] allowExt = new string[] { ".gif", ".jpg", ".png", ".jpeg", ".bmp", ".rar", ".rz" }; //for (int i = 0; i < allowExt.Length; i++) //{ // if (allowExt[i] == _ImageExt) { return true; } //} StringComparer stringComparer = StringComparer.OrdinalIgnoreCase; return allowExt.Any(c => stringComparer.Equals(c, imageExt)); } private string GetImageName() { Random rd = new Random(); StringBuilder serial = new StringBuilder(); serial.Append(DateTime.Now.ToString("yyyyMMddHHmmssff")); serial.Append(rd.Next(0, 999999).ToString()); return serial.ToString(); } public int DefaultHeight { get { //此处硬编码了,可以写入配置文件中。 return 200; } } #endregion public bool IsReusable { get { return false; } } } }