Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%@ WebHandler Language="C#" Class="FileUploadHandler" %>
- using System;
- using System.IO;
- using System.Net;
- using System.Web;
- using System.Web.Script.Serialization;
- public class FileUploadHandler : IHttpHandler
- {
- public void ProcessRequest(HttpContext context)
- {
- context.Response.ContentType = "text/plain";
- string unique_id = "", folder_name = "";
- if (context.Request.Form["unique_id"].ToString() != null && !String.IsNullOrEmpty(context.Request.Form["unique_id"].ToString()))
- {
- unique_id = context.Request.Form["unique_id"].ToString();
- folder_name = context.Request.Form["folder_name"].ToString();
- UploadFile(context, unique_id, folder_name);//to identify user id and control id
- }
- else
- {
- context.Response.Write("Invalid Call");
- }
- }
- #region UploadFile
- public void UploadFile(HttpContext context, string unique_id, string folder_name)
- {
- string base_dir = AppDomain.CurrentDomain.BaseDirectory;//Base Directory
- CreateDirectoryIfNotExists(HttpContext.Current.Application["BASE_DIR"] + "\\TempFile\\" + folder_name + "\\");
- string str_image = "";
- HttpPostedFile file = context.Request.Files["file"];
- string fileName = file.FileName;
- string fileExtension = Path.GetExtension(fileName);
- if (!string.IsNullOrEmpty(fileName))
- {
- fileExtension = Path.GetExtension(fileName);
- //here @ key add to identify unique key porsion from file name use in delete of this catagory file
- str_image = unique_id + fileExtension;
- string filePath = Handle.ToString(HttpContext.Current.Application["BASE_DIR"] + "\\TempFile\\" + folder_name + "\\" + str_image);
- file.SaveAs(HttpContext.Current.Application["BASE_DIR"] + "\\TempFile\\" + folder_name + "\\" + str_image);
- }
- //pass image name for database update logic
- context.Response.Write(str_image);
- }
- #endregion
- #region CreateDirectoryIfNotExists
- private Boolean CreateDirectoryIfNotExists(string NewDirectory)
- {
- try
- {
- // Checking the existance of directory
- if (!Directory.Exists(NewDirectory))
- {
- //If No any such directory then creates the new one
- Directory.CreateDirectory(NewDirectory);
- return true;
- }
- else
- {
- return true;
- }
- }
- catch (IOException _err)
- {
- return false;
- }
- }
- #endregion
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement