我遇到会话问题,我的处理程序没有读取会话值。这只发生在我们的服务器上。当我运行本地时,它工作正常。我也在调用IRequiresSessionState,因此也不应该成为问题。
我能够看到会话状态仍然有效,直到我调用我的ashx文件。然而,根据我的踪迹,在这个文件中,会话丢失了。
这是我用来调用我的处理程序的javascript代码:
$(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'
});
下面是我的处理程序.cs文件代码
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);
}
每次进入其他部分并根据代码创建新文件夹。 请建议
答案 0 :(得分:0)
我得到了在ASJX文件中处理会话的替代方法 我在&#39; scriptData&#39;:{&#39; SessionData&#39;:currentDirectory}中传递会话,标记为uploadify函数并以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'
});
我们已使用以下行
在onComplete
事件中设置了会话数据
$("#<%=btnAdd.ClientID%>").uploadifySettings('scriptData', { 'SessionData': currentDirectory });
并在c#中获取数据,如下所示
string sessionValue = Convert.ToString(context.Request.Form["SessionData"])