| <%@ WebHandler Language="C#" Class="TestHandler" %> |
| |
| using System; |
| using System.Web; |
| using System.Collections; |
| using System.Collections.Generic; |
| using System.Linq; |
| using System.Data.Linq; |
| using System.IO; |
| |
| public class TestHandler : Telerik.Windows.RadUploadHandler |
| { |
| protected Dictionary<string, object> _customValues; |
| |
| protected string BuildPhysicalDirectory(ArrayList subDirs, bool isPublic) |
| { |
| try |
| { |
| if (subDirs == null) return null; |
| string baseDir = "App_Data"; |
| if (isPublic) |
| baseDir = "Data"; |
| string currPhysPath = System.IO.Path.Combine(this.Context.Request.PhysicalApplicationPath, baseDir); |
| foreach (string dir in subDirs) |
| { |
| currPhysPath = System.IO.Path.Combine(currPhysPath, dir); |
| } |
| return currPhysPath; |
| } |
| catch (System.Web.HttpException e) |
| { |
| jt_Error_Log error = new jt_Error_Log(); |
| error.Description = "Could not build phys path"; |
| error.Error_Code = -1; |
| error.Error_Date = DateTime.Now; |
| error.Module_Name = "Upload Handler"; |
| error.Filename = "TestHandler.ashx"; |
| |
| JtDataContext db = new JtDataContext(); |
| db.jt_Error_Logs.InsertOnSubmit(error); |
| db.SubmitChanges(); this.AddReturnParam("message", e.Message); |
| return null; |
| } |
| } |
| |
| protected string BuildRelativeDirectory(ArrayList subDirs, bool isPublic) |
| { |
| try |
| { |
| if (subDirs == null) return null; |
| string baseDir = "~/App_Data"; |
| if (isPublic) |
| baseDir = "~/Data"; |
| string currPhysPath = baseDir; |
| foreach (string dir in subDirs) |
| { |
| currPhysPath = System.IO.Path.Combine(currPhysPath, dir); |
| } |
| return currPhysPath; |
| } |
| catch (System.Web.HttpException e) |
| { |
| jt_Error_Log error = new jt_Error_Log(); |
| error.Description = "Could not build relative dir"; |
| error.Error_Code = -1; |
| error.Error_Date = DateTime.Now; |
| error.Module_Name = "Upload Handler"; |
| error.Filename = "TestHandler.ashx"; |
| |
| JtDataContext db = new JtDataContext(); |
| db.jt_Error_Logs.InsertOnSubmit(error); |
| db.SubmitChanges(); this.AddReturnParam("message", e.Message); |
| return null; |
| } |
| } |
| |
| protected bool EnsureDirectoryStructExists(HttpContext context, ArrayList subDirs, bool isPublic) |
| { |
| string baseDir = "App_Data"; |
| if (isPublic) |
| baseDir = "Data"; |
| bool exists = false; |
| |
| try |
| { |
| string currPhysPath = System.IO.Path.Combine(context.Request.PhysicalApplicationPath,baseDir); |
| // Make sure the directory structure exists |
| DirectoryInfo di = new DirectoryInfo(currPhysPath); |
| // Create the directory only if it does not already exist. |
| if (!di.Exists) |
| di.Create(); |
| |
| foreach(string dir in subDirs) |
| { |
| currPhysPath = System.IO.Path.Combine(currPhysPath, dir); |
| |
| DirectoryInfo diSub = new DirectoryInfo(currPhysPath); |
| // Create the directory only if it does not already exist. |
| if (!diSub.Exists) |
| diSub.Create(); |
| } |
| } |
| catch (System.Web.HttpException e) |
| { |
| this.AddReturnParam("message", e.Message); |
| |
| jt_Error_Log error = new jt_Error_Log(); |
| error.Description = "Could not write path"; |
| error.Error_Code = -1; |
| error.Error_Date = DateTime.Now; |
| error.Module_Name = "Upload Handler"; |
| error.Filename = "TestHandler.ashx"; |
| |
| JtDataContext db = new JtDataContext(); |
| db.jt_Error_Logs.InsertOnSubmit(error); |
| db.SubmitChanges(); |
| return false; |
| } |
| return true; |
| } |
| |
| public override Dictionary<string, object> GetAssociatedData() |
| { |
| return _customValues; |
| } |
| |
| /// <summary> |
| /// Override the target folder based on the type. |
| /// </summary> |
| /// <returns></returns> |
| public override string GetTargetFolder() |
| { |
| string fileType = this.Request.Form["JT_FileType"]; |
| string dcName = this.Request.Form["JT_Dc"]; |
| |
| // If it's a custom file type |
| if (!string.IsNullOrEmpty(fileType)) |
| { |
| ArrayList alDirs = new ArrayList(); |
| alDirs.Add(dcName); |
| if ((fileType.Equals("Category")) && (!string.IsNullOrEmpty(dcName))) |
| { |
| alDirs.Add("Categories"); |
| alDirs.Add("images"); |
| if (IsNewFileRequest()) |
| { |
| if (!EnsureDirectoryStructExists(this.Context, alDirs, true)) |
| { |
| jt_Error_Log error = new jt_Error_Log(); |
| error.Description = "Directory could not be created"; |
| error.Error_Code = -1; |
| error.Error_Date = DateTime.Now; |
| error.Module_Name = "Upload Handler"; |
| error.Filename = "TestHandler.ashx"; |
| |
| JtDataContext db = new JtDataContext(); |
| db.jt_Error_Logs.InsertOnSubmit(error); |
| db.SubmitChanges(); |
| } |
| return BuildPhysicalDirectory(alDirs, true); |
| } |
| else |
| return BuildPhysicalDirectory(alDirs, true); |
| } |
| else |
| return base.GetTargetFolder(); |
| } |
| |
| // Otherwise do the default behavior. |
| else |
| { |
| return base.GetTargetFolder(); |
| } |
| } |
| |
| public override void ProcessStream() |
| { |
| if (this.IsFinalFileRequest()) |
| { |
| string fileType = this.Request.Form["JT_FileType"]; |
| string dcName = this.Request.Form["JT_Dc"]; |
| if (!string.IsNullOrEmpty(fileType)) |
| { |
| if (fileType.Equals("Category")) |
| { |
| string sCategoryId = this.Request.Form["JT_CategoryId"]; |
| int categoryId = 0; |
| if (int.TryParse(sCategoryId, out categoryId)) |
| { |
| JtDataContext db = new JtDataContext(); |
| jt_Category cat = (from c in db.jt_Categories |
| where c.CategoryId == categoryId |
| select c).Single(); |
| ArrayList alDirs = new ArrayList(); |
| alDirs.Add(dcName); |
| alDirs.Add("Categories"); |
| alDirs.Add("images"); |
| |
| cat.ImageFile = Path.Combine(BuildRelativeDirectory(alDirs, true), this.Request.Params["RadUAG_FileName"]); |
| db.SubmitChanges(); |
| _customValues = new Dictionary<string, object>(); |
| _customValues.Add("JT_FileName", WcfServiceUtils.ResolveServerImgUrl(Path.Combine(BuildRelativeDirectory(alDirs, true), this.Request.Params["RadUAG_FileName"]))); |
| } |
| } |
| } |
| base.ProcessStream(); |
| } |
| else |
| { |
| base.ProcessStream(); |
| } |
| } |
| } |