简体   繁体   中英

Session state not working in ashx file on server

I have a session issue, where my handler is not reading the session values. This ONLY occurs on our server. When I run local, it works fine. I'm also calling IRequiresSessionState, so that shouldn't be an issue either.

I'm able to see the session state is still working up until I call my ashx file. Inside this file however, the session is lost, according to my trace.

Here's my javascript code I use to call my handler:

$(document).ready(function () {
        $("#<%=btnAdd.ClientID%>").uploadify({
            'uploader': '../Scripts/Uploadify/uploadify.swf',
            'script': '../Handlers/file1.ashx?mode=schedule',
            'cancelImg': '../Images/cancel.png',
            'wmode': 'transparent',
            'hideButton': true,
            'fileExt': '*.XML;*.xml;*.CIF;*.cif;*.zip;*.ZIP',
            'fileDesc': 'Schedule Files',
            'onComplete': function (event, queueID, fileObj, response, data) {
                $('#pnlOverlayFrame').show();
                document.getElementById("<%=hdnFilePath.ClientID%>").value = response;
            },
            'onAllComplete': function (event, queueID, fileObj, response, data) {
                $get('<%= hdnDirty.ClientID %>').value = '0';
                //                    document.getElementById('<%= btnConfirm.ClientID %>').click();
                document.getElementById('<%= btnUploadFiles.ClientID %>').click();
            }

            ,
            'multi': true,
            'expressInstall': '../Scripts/Uploadify/expressInstall.swf'
        });

Below is my handler .cs file code

try
            {
                HttpPostedFile postedFile = context.Request.Files["Filedata"];
                if (context.Session != null && context.Session["CurrentDirectory"] != null)
                {
                    StorageRoot = context.Server.MapPath(context.Session["CurrentDirectory"].ToString());
                }
                else
                {
                    //string DirectoryName = "OP1" + "_" + DateTime.Now.ToString().Replace('/', '_').Replace(':', '_').Replace(' ', '_') + Guid.NewGuid().ToString();
                    string DirectoryName = "OP1" + "_" + String.Format("{0:yyyyMdHHmmss}", DateTime.Now) + Guid.NewGuid().ToString();

                    //DirectoryName = DirectoryName.Remove(DirectoryName.Length - 3, 3);
                    HttpContext.Current.Session.Add("CurrentDirectory", DirectoryName);
                    //context.Session["CurrentDirectory"] = DirectoryName;
                    StorageRoot = context.Server.MapPath(DirectoryName);
                }


                string filename = postedFile.FileName;
                if (!Directory.Exists(StorageRoot))
                    Directory.CreateDirectory(StorageRoot);

                postedFile.SaveAs(StorageRoot + @"\" + filename);
                context.Response.Write(StorageRoot);
                context.Response.StatusCode = 200;

            }
            catch (Exception ex)
            {
                context.Response.Write("Error: " + ex.Message);
            }

Every time it goes in else part and create new folder as per code. Please advice

I got the alternate way of handling session in ASJX file i have pass session in 'scriptData': { 'SessionData': currentDirectory }, tag in to uploadify function and get this data in term of Form

JavascriptCode

 var currentDirectory = "";
    $(document).ready(function () {                      
        $("#<%=btnAdd.ClientID%>").uploadify({                      
            'uploader': '../Scripts/Uploadify/uploadify.swf',
            'script': '../Handlers/file1.ashx?mode=schedule',
            'scriptData': { 'SessionData': currentDirectory },
            'cancelImg': '../Images/cancel.png',
            'wmode': 'transparent',
            'hideButton': true,
            'fileExt': '*.XML;*.xml;*.CIF;*.cif;*.zip;*.ZIP',
            'fileDesc': 'Schedule Files',
            'onComplete': function (event, queueID, fileObj, response, data) {
                $('#pnlOverlayFrame').show();
                document.getElementById("<%=hdnFilePath.ClientID%>").value = response;
                currentDirectory = response;
                $("#<%=btnAdd.ClientID%>").uploadifySettings('scriptData', { 'SessionData': currentDirectory });
            },
            'onAllComplete': function (event, queueID, fileObj, response, data) {
                $get('<%= hdnDirty.ClientID %>').value = '0';
                //                    document.getElementById('<%= btnConfirm.ClientID %>').click();
                document.getElementById('<%= btnUploadFiles.ClientID %>').click();     
                currentDirectory = "";
            }

            ,
            'multi': true,
            'expressInstall': '../Scripts/Uploadify/expressInstall.swf'
        });

we have set session data in onComplete event with use of below line

$("#<%=btnAdd.ClientID%>").uploadifySettings('scriptData', { 'SessionData': currentDirectory });

and get data in c# like below

string sessionValue = Convert.ToString(context.Request.Form["SessionData"])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM