using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO; using System.Web.Script.Serialization; using System.Drawing; namespace NetWorkHard.GlobalAshx { /// /// AjaxFileUpdate 的摘要说明 /// public class AjaxFileUpdate : IHttpHandler { public void ProcessRequest(HttpContext context) { //context.Response.ContentType = "text/plain"; //context.Response.Charset = "utf-8"; HttpFileCollection files = context.Request.Files; string DirectoryName = context.Request.QueryString["DirectoryName"]; string width = context.Server.UrlDecode(context.Request.QueryString["width"]); string height = context.Server.UrlDecode(context.Request.QueryString["height"]); if (string.IsNullOrEmpty(DirectoryName) == true) DirectoryName = "ServerCookies"; DirectoryName = System.AppDomain.CurrentDomain.BaseDirectory + DirectoryName; if (Directory.Exists(DirectoryName) == false) Directory.CreateDirectory(DirectoryName); if (files.Count > 0) { string ExtensionName = Path.GetExtension(files[0].FileName).ToLower(); string filename = Guid.NewGuid().ToString() + ExtensionName; var model = new { FileName = filename, FileSize = files[0].InputStream.Length }; string jsonString = new JavaScriptSerializer().Serialize(model); context.Response.Write(jsonString); if (width != null && height != null) { string filepath = System.AppDomain.CurrentDomain.BaseDirectory + "ServerCookies/" + filename; files[0].SaveAs(filepath); Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback); Bitmap myBitmap = new Bitmap(filepath); int thumbWidth = Convert.ToInt32(width); int thumbHeight = Convert.ToInt32(height); Image myThumbnail = myBitmap.GetThumbnailImage(thumbWidth, thumbHeight, myCallback, IntPtr.Zero); Bitmap bmp1 = new Bitmap(myThumbnail); string filepath2 = DirectoryName + "/" + filename; bmp1.Save(filepath2, System.Drawing.Imaging.ImageFormat.Png); bmp1.Dispose(); myBitmap.Dispose(); File.Delete(filepath); ////20240716新增 如果文件保存的信息不存在,返回0告知 if (!File.Exists(filepath2)) { context.Response.Clear(); context.Response.Write("0"); } ////20240716end } else { string filepath = DirectoryName + "/" + filename; files[0].SaveAs(filepath); ////20240716新增 如果文件保存的信息不存在,返回0告知 if (!File.Exists(filepath)) { context.Response.Clear(); context.Response.Write("0"); } ////20240716end } context.Response.End(); } else { context.Response.Write("0"); } } public bool ThumbnailCallback() { return false; } public bool IsReusable { get { return false; } } } }