使用FileUpload1.SaveAs后保留会话值 [英] Retain Session values after using FileUpload1.SaveAs

查看:219
本文介绍了使用FileUpload1.SaveAs后保留会话值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用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?

推荐答案

I由于SaveAs方法,没有理由失去会话。

在SaveAs之前和之后检查一个小文件,看看它是否真的是原因。


如果您丢失了会话,可能会超时,或者您的代码或应用生命周期中的某个其他位置...



干杯,

Edo
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


这篇关于使用FileUpload1.SaveAs后保留会话值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆