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)
{
if (Get_and_Show_FileInformation())
{
}
else
{
}
}
}
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))
{
try
{
FileUpload1.SaveAs(Server.MapPath("~/Uploads/" + dir + "/" + FileName));
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
{
}
}
}
I am not sure what is the reason for this, any ideas, work around?