So set <session cokieless="UseCookies"> in web.config.
As long as you have IRequreSessionState then the handlers should have session available. Check the HTTP request using fiddler to make sure the cookie is sent.
I solve my problem with using my own RND...and its hole day RND...But at finaly i Get Solution.. and i put it here for others which are getting this type of problems....
This is My final handler and i go with cookieless="false" in web.config file.......
<%@ WebHandler Language="C#" class="Handler" %>
using System.Collections.Generic;
using System;
using System.Drawing;
using System.Web;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.SessionState;
public class Handler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
//delete uploaded file if we call this page and pass query sting with the name of the file
if (context.Request.QueryString.Count > 0)
{
string filePath = HttpContext.Current.Server.MapPath("uploads") + "//" + context.Request.QueryString[0].ToString();
if (File.Exists(filePath))
File.Delete(filePath);
}
// if we are not passing the name of the file then we are uploading ..
else
{
string savepath = "";
string tempPath = "";
tempPath = ConfigurationManager.AppSettings["FolderPath"];
savepath = context.Server.MapPath(tempPath);
if (!Directory.Exists(savepath))
Directory.CreateDirectory(savepath);
var ext = System.IO.Path.GetExtension(context.Request.Files[0].FileName); // get file extention
string fileName = Path.GetFileName( context.Request.Files[0].FileName); // take the name of the uploaded file .
if (context.Request.Files[0].FileName.LastIndexOf("\\") != -1)
{
fileName = context.Request.Files[0].FileName.Remove(0, context.Request.Files[0].FileName.LastIndexOf("\\")).ToLower(); // remove extra folder path (IE bug)
}
fileName = GetUniqueFileName(fileName, HttpContext.Current.Server.MapPath("uploads/") , ext).ToLower(); // get unique name
string location = HttpContext.Current.Server.MapPath("uploads/") + fileName + ext; //save path
context.Request.Files[0].SaveAs(location); // save the file
context.Response.End();
}
}
public static string GetUniqueFileName(string name, string savePath, string ext)
{
name = name.Replace(ext, "").Replace(" ", "_"); // remove extension and spaces from the name
name = System.Text.RegularExpressions.Regex.Replace(name, @"[^\w\s]", ""); // remove all punctuations
var newName = name;
var i = 0;
if (System.IO.File.Exists(savePath + newName + ext)) // check if the file name already there
{
do
{
i++;
newName = name + "_" + i;
}
while (System.IO.File.Exists(savePath + newName + ext));
}
return newName; // return new name
}
public bool IsReusable {
get {
return false;
}
}
}
BrockAllen
All-Star
27512 Points
4895 Posts
MVP
Re: session value in handler is null
Nov 08, 2012 05:20 PM|LINK
So set <session cokieless="UseCookies"> in web.config.
As long as you have IRequreSessionState then the handlers should have session available. Check the HTTP request using fiddler to make sure the cookie is sent.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
Renish Ankol...
0 Points
10 Posts
Re: session value in handler is null
Nov 08, 2012 05:31 PM|LINK
hello sir i am new, pls give me example how to do it and how to solve my problem...as per your seggetion what i do for solve my problem?
BrockAllen
All-Star
27512 Points
4895 Posts
MVP
Re: session value in handler is null
Nov 08, 2012 05:56 PM|LINK
Read the docs :)
http://msdn.microsoft.com/en-us/library/ms178581%28v=vs.100%29.aspx
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
Renish Ankol...
0 Points
10 Posts
Re: session value in handler is null
Nov 08, 2012 06:07 PM|LINK
thanks for yur reply but i am not opening your link,
i get error ..
The specified CGI application encountered an error and the server terminated the process.
Renish Ankol...
0 Points
10 Posts
Re: session value in handler is null
Nov 08, 2012 08:13 PM|LINK
Thanks for your valueable reply and support...
I solve my problem with using my own RND...and its hole day RND...But at finaly i Get Solution.. and i put it here for others which are getting this type of problems....
This is My final handler and i go with cookieless="false" in web.config file.......
<%@ WebHandler Language="C#" class="Handler" %>
using System.Collections.Generic;
using System;
using System.Drawing;
using System.Web;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.SessionState;
public class Handler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
//delete uploaded file if we call this page and pass query sting with the name of the file
if (context.Request.QueryString.Count > 0)
{
string filePath = HttpContext.Current.Server.MapPath("uploads") + "//" + context.Request.QueryString[0].ToString();
if (File.Exists(filePath))
File.Delete(filePath);
}
// if we are not passing the name of the file then we are uploading ..
else
{
string savepath = "";
string tempPath = "";
tempPath = ConfigurationManager.AppSettings["FolderPath"];
savepath = context.Server.MapPath(tempPath);
if (!Directory.Exists(savepath))
Directory.CreateDirectory(savepath);
var ext = System.IO.Path.GetExtension(context.Request.Files[0].FileName); // get file extention
string fileName = Path.GetFileName( context.Request.Files[0].FileName); // take the name of the uploaded file .
if (context.Request.Files[0].FileName.LastIndexOf("\\") != -1)
{
fileName = context.Request.Files[0].FileName.Remove(0, context.Request.Files[0].FileName.LastIndexOf("\\")).ToLower(); // remove extra folder path (IE bug)
}
fileName = GetUniqueFileName(fileName, HttpContext.Current.Server.MapPath("uploads/") , ext).ToLower(); // get unique name
string location = HttpContext.Current.Server.MapPath("uploads/") + fileName + ext; //save path
context.Request.Files[0].SaveAs(location); // save the file
// string location = savepath + @"\" + fileName + ext;;
string completepath = tempPath + "/" + fileName + ext;
context.Response.Write(completepath); // return saved name
context.Response.StatusCode = 200;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestqkConnectionString"].ConnectionString);
con.Open();
string UserId = context.Session["UserID"].ToString();
SqlCommand cmd1 = new SqlCommand("SELECT DISTINCT Profile.ProfileID FROM Profile INNER JOIN [User] ON Profile.ProfileID = [User].P_PID where [user].UserID = " + UserId + "", con);
Object result = cmd1.ExecuteScalar();
string ProfileID = String.Format("{0:G}", result);
SqlCommand cmd = new SqlCommand("Insert into ProfileImage(ImageName,ImageURL,P_PID) values(@ImageName,@ImagePath,@P_PID)", con);
cmd.Parameters.AddWithValue("@ImageName", fileName);
cmd.Parameters.AddWithValue("@ImagePath", completepath);
cmd.Parameters.AddWithValue("@P_PID", ProfileID);
cmd.ExecuteNonQuery();
con.Close();
context.Response.End();
}
}
public static string GetUniqueFileName(string name, string savePath, string ext)
{
name = name.Replace(ext, "").Replace(" ", "_"); // remove extension and spaces from the name
name = System.Text.RegularExpressions.Regex.Replace(name, @"[^\w\s]", ""); // remove all punctuations
var newName = name;
var i = 0;
if (System.IO.File.Exists(savePath + newName + ext)) // check if the file name already there
{
do
{
i++;
newName = name + "_" + i;
}
while (System.IO.File.Exists(savePath + newName + ext));
}
return newName; // return new name
}
public bool IsReusable {
get {
return false;
}
}
}
And best Link for this is....
http://engthunder.wordpress.com/2012/04/16/uploading-file-using-jquery-with-generic-handler-ashx/
Thanks a lot to all....