<%@ WebHandler Language="C#" Class="Upload" %> using System; using System.Web; using System.IO; using System.Drawing; using System.Drawing.Imaging; using CandidateApp.Web; public class Upload : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Expires = -1; try { // foreach added for jquery file upload. for uploadify remove forearch loop. foreach (string file in context.Request.Files) { HttpPostedFile postedFile = context.Request.Files[file] as HttpPostedFile; //context.Request.Files["Filedata"]; bool replaceInvalidChars = true; if (context.Request["replaceInvalidChars"] != null && context.Request["replaceInvalidChars"].ToString() != "") { bool.TryParse(context.Request["replaceInvalidChars"].ToString(), out replaceInvalidChars); } bool overwrite = false; if (context.Request["overwrite"] != null && context.Request["overwrite"].ToString() != "") { bool.TryParse(context.Request["overwrite"].ToString(), out overwrite); } //check image file section [start] bool isImageFile = false; bool checkImageFile = false; if (context.Request["checkImageFile"] != null && context.Request["checkImageFile"].ToString() != "") { bool.TryParse(context.Request["checkImageFile"].ToString(), out checkImageFile); } if (checkImageFile) { Stream streamObj = postedFile.InputStream; var i = Image.FromStream(streamObj); streamObj.Seek(0, SeekOrigin.Begin); if (ImageFormat.Jpeg.Equals(i.RawFormat) || ImageFormat.Png.Equals(i.RawFormat) || ImageFormat.Gif.Equals(i.RawFormat)) { isImageFile = true; } if (isImageFile) { System.Drawing.Image oImage = System.Drawing.Image.FromStream(streamObj); int height = 0; int width = 0; //int.TryParse(GblConfiSettings.ResizeHeight, out height); //int.TryParse(GblConfiSettings.ResizeWidth, out width); if (oImage.Height > height && oImage.Width > width) { string folderPath = ""; if (context.Request["folderpath"] != null && context.Request["folderpath"].ToString() != "") { folderPath = context.Request["folderpath"].ToString(); } string savepath = ""; if (folderPath == "") { savepath = context.Server.MapPath("~/" + GlobalConfigurationSettings.JQFileUploadFolderPath); } else { savepath = context.Server.MapPath("~/" + folderPath); } string filename = postedFile.FileName.GetPhysicalFileName(savepath, overwrite, replaceInvalidChars); if (!Directory.Exists(savepath)) Directory.CreateDirectory(savepath); postedFile.SaveAs(savepath + @"\" + filename); context.Response.Write(filename); //return physicalfilename context.Response.StatusCode = 200; } else { context.Response.Write("IHWF"); } } else { context.Response.Write("IIF"); } }//check image file section [end] else { string folderPath = ""; if (context.Request["folderpath"] != null && context.Request["folderpath"].ToString() != "") { folderPath = context.Request["folderpath"].ToString(); } string savepath = ""; if (folderPath == "") { savepath = context.Server.MapPath("~/" + GlobalConfigurationSettings.JQFileUploadFolderPath); } else { savepath = context.Server.MapPath("~/" + folderPath); } string filename = postedFile.FileName.GetPhysicalFileName(savepath, overwrite, replaceInvalidChars); if (!Directory.Exists(savepath)) Directory.CreateDirectory(savepath); postedFile.SaveAs(savepath + @"\" + filename); context.Response.Write(filename); //return physicalfilename context.Response.StatusCode = 200; } } } catch (Exception) { //context.Response.Write("Error: " + ex.Message); context.Response.Write("DBE"); } } public bool IsReusable { get { return false; } } }