问题描述
在使用FileUpload控件的.SaveAS方法后,我遇到会话变量丢失。
我想将实际文件上传到FTP服务器所以我是使用以下代码。但是在使用.SaveAs方法之后,在第二次或第三次回发时,会话值会丢失。
在页面加载,转换会话(从登录页面设置)作为clsUser
I am experiencing loss of session variables after using .SaveAS method of FileUpload control.
I want to physically upload the actual file to the FTP server so I am using following code. But after using .SaveAs method, on second or third postback, session values are lost.
On page load, casting session (that was set from login page) as clsUser
protected void Page_Load(object sender, EventArgs e)
{
if (Session["TheUser"] != null)
{
aUser = (clsUser)Session["TheUser"];
}
}
点击按钮
on button click
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)//continue only if the file control has the file
{
if (Get_and_Show_FileInformation())//if the file information was retrieved
{
//continue with the execution
}
else
{
//error message displyed
}
}
}
此函数用于获取,显示和上传文件到FTP服务器。在这里,我使用
This function is used to get, show and upload file to FTP serer. Here I am using
.SaveAs
导致会话值丢失的方法。
method that causes the loss of session values.
protected bool Get_and_Show_FileInformation()
{
if (FileUpload1.HasFile)
{
string fName = FileUpload1.PostedFile.FileName;
string extension = Path.GetExtension(fName);
string FileName = fName.Substring(0, fName.Length - extension.Length);
string dir = uname;
string appPath = Server.MapPath("Uploads/") + dir;
FileInfo MyFileInfo = new FileInfo(appPath);
DirectoryInfo newDirectoryInfo = new DirectoryInfo(appPath);
if (!Directory.Exists(appPath))//FTP_Upload first time, create a directory
{
try
{
FileUpload1.SaveAs(Server.MapPath("~/Uploads/" + dir + "/" + FileName)); // ERROR here, call to this .SaveAs method causes loss of session values (Session["TheUser"])
Image2.ImageUrl = "~/Uploads/" + dir + "/" + FileName;
return true;
}
catch (Exception ex)
{
lbl_Err.Text = "<br/>Error: Error creating and saving into your space!(Error Code:XF05)";
return false;
}
}
else
{
//same code but don't create the directory
}
}
}
我不知道是什么原因,任何想法,解决?
I am not sure what is the reason for this, any ideas, work around?
推荐答案
这篇关于使用FileUpload1.SaveAs后保留会话值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!