Advertisement
ardann

Untitled

Apr 25th, 2023 (edited)
939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1. <%@ WebHandler Language="C#" Class="FileUploadHandler" %>
  2.  
  3. using System;
  4. using System.IO;
  5. using System.Net;
  6. using System.Web;
  7. using System.Web.Script.Serialization;
  8.  
  9. public class FileUploadHandler : IHttpHandler
  10. {
  11.     public void ProcessRequest(HttpContext context)
  12.     {
  13.         context.Response.ContentType = "text/plain";
  14.         string unique_id = "", folder_name = "";
  15.         if (context.Request.Form["unique_id"].ToString() != null && !String.IsNullOrEmpty(context.Request.Form["unique_id"].ToString()))
  16.         {
  17.             unique_id = context.Request.Form["unique_id"].ToString();
  18.             folder_name = context.Request.Form["folder_name"].ToString();
  19.             UploadFile(context, unique_id, folder_name);//to identify user id   and control id
  20.         }
  21.         else
  22.         {
  23.             context.Response.Write("Invalid Call");
  24.         }
  25.     }
  26.  
  27.     #region UploadFile
  28.     public void UploadFile(HttpContext context, string unique_id, string folder_name)
  29.     {
  30.         string base_dir = AppDomain.CurrentDomain.BaseDirectory;//Base Directory
  31.         CreateDirectoryIfNotExists(HttpContext.Current.Application["BASE_DIR"] + "\\TempFile\\" + folder_name + "\\");
  32.         string str_image = "";
  33.        
  34.         HttpPostedFile file = context.Request.Files["file"];
  35.         string fileName = file.FileName;
  36.         string fileExtension = Path.GetExtension(fileName);
  37.  
  38.         if (!string.IsNullOrEmpty(fileName))
  39.         {
  40.             fileExtension = Path.GetExtension(fileName);
  41.             //here @ key add to identify unique key porsion from file name use in delete of this catagory file
  42.             str_image = unique_id + fileExtension;
  43.             string filePath = Handle.ToString(HttpContext.Current.Application["BASE_DIR"] + "\\TempFile\\" + folder_name + "\\" + str_image);
  44.  
  45.             file.SaveAs(HttpContext.Current.Application["BASE_DIR"] + "\\TempFile\\" + folder_name + "\\" + str_image);
  46.         }
  47.  
  48.         //pass image name for database update logic
  49.         context.Response.Write(str_image);
  50.     }
  51.     #endregion
  52.  
  53.     #region CreateDirectoryIfNotExists
  54.     private Boolean CreateDirectoryIfNotExists(string NewDirectory)
  55.     {
  56.         try
  57.         {
  58.             // Checking the existance of directory
  59.             if (!Directory.Exists(NewDirectory))
  60.             {
  61.                 //If No any such directory then creates the new one
  62.                 Directory.CreateDirectory(NewDirectory);
  63.                 return true;
  64.             }
  65.             else
  66.             {
  67.  
  68.                 return true;
  69.             }
  70.         }
  71.         catch (IOException _err)
  72.         {
  73.             return false;
  74.         }
  75.     }
  76.     #endregion
  77.  
  78.     public bool IsReusable
  79.     {
  80.         get
  81.         {
  82.             return false;
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement