Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (Session["TheUser"] != null)
    {
        aUser = (clsUser)Session["TheUser"];
    }
}


on button click
C#
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
        }
    }
}

This function is used to get, show and upload file to FTP serer. Here I am using
C#
.SaveAs
method that causes the loss of session values.
C#
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?
Posted
Comments
Karthik Harve 10-Apr-13 5:04am    
what is the session timeout you specified in web.config file ?
Sumit Bharadia 10-Apr-13 5:12am    
10 min
Karthik Harve 10-Apr-13 5:17am    
how are you checking whether the session has expired or not ?
Sumit Bharadia 10-Apr-13 5:12am    
the file size is no more than 1MB
N ManojKumar 10-Apr-13 5:18am    
I think you missed !IsPostBack poperty in your page load method.

1 solution

I see no reason for the loss of the session due to the SaveAs method.
check with a small file right before and after the SaveAs to see if it is actually the cause.

If you lose session it might be timeout, or some other action someplace else in your code or app lifecycle...

Cheers,
Edo
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900