简体   繁体   中英

Problem with retrieveing file upload control

I saved fileupload control in session. But when I am retrieving in another page(ie to know whether it has file or not),it is showing error as "object instances not set to an object". Where can be the fault? The code I used to get that fileupload control is

{
   Fileupload myupload=(Fileupload)Session["Fileupload1"];
   if(myupload.HasFile)
    {
          //some code
    }
}

Well that's not going to work. You can't put controls in session state. You'll need to process the file upload on the page that received the post. Then you'll need to save off the file to a temporary directory or something on the server.

I'd go back to the drawing board and try another approach.

Without seeing the actual stack trace, it looks like Session["Fileupload1"] is null.

I would say that if you are saving the fileUpload to session, it's maybe not the best way to tackle your given problem, of which we know little about.

That said, you should check to see if an object is in session before casting it as something in case it has been lost.

if (Session["MySessionVar"] != null)
{
      <type> myVar = (<type>)Session["MySessionVar"]; 
}
else
{
      // set default/write warning to log/warn user
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM