首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

FileUpload 未将对像引用到实例,马上结解决方法

2012-03-28 
FileUpload 未将对像引用到实例,马上结protectedvoidButton1_Click(objectsender,EventArgse){if(User.Ide

FileUpload 未将对像引用到实例,马上结
protected   void   Button1_Click(object   sender,   EventArgs   e)
        {
                if   (User.Identity.Name   ==   " ")
                {
                        Response.Write( " <script> window.alert( '登录超时,请重新登录 ');window.location= '/user/login.aspx '; </script> ");
                        return;
                }
                string   filename   =   FileUpload1.PostedFile.FileName;   //取得文件名
                int   i   =   filename.LastIndexOf( ". ");
                string   newext   =   filename.Substring(filename.LastIndexOf( ". ")   +   1).ToLower();   //取得文件扩展名
                //生成新的图片名
                string   NewFileName   =   FileUpload1.PostedFile.ContentLength.ToString()   +   "_ "   +   DateTime.Now.ToString( "yyyyMMddhhmmss ")   +   ". "   +   newext;
                string   dateming   =   DateTime.Now.ToString( "yyyy-MM-dd ");   //取得日期(创建目录)
                if   (newext   !=   "jpg "   &&   newext   !=   "bmp "   &&   newext   !=   "gif "   &&   newext   !=   "jpge ")
                {
                        Response.Write( " <script> window.alert( '文件格式不正确,请检查文件类型。 '); </script> ");
                        return;
                }
                if   (FileUpload1.PostedFile.ContentLength   > =   512000)
                {
                        Response.Write( " <script> window.alert( '图片太大!请控制在500K以下! '); </script> ");
                        return;
                }
                string   rootpath   =   Server.MapPath( "/images/Up/ "   +   dateming);
                if   (!Directory.Exists(rootpath))
                {
                        System.IO.Directory.CreateDirectory(rootpath);
                }
                string   serverpath   =   Server.MapPath( "/images/Up/ "   +   dateming   +   "/ "   +   NewFileName);


                FileUpload1.PostedFile.SaveAs(serverpath);
                string   Url   =   "/images/Up/ "   +   dateming   +   "/ "   +   NewFileName;


                Location.Value   =   Url;
                Image1.ImageUrl   =   Url;
        }


-----------------------------------
  string   filename   =   FileUpload1.PostedFile.FileName;   //取得文件名
这句出错...但是在其它页面用相同的代码就不会出错...我用了UpdatePanel控件

[解决办法]
UpdatePanel是ajax控件吧,里面不能用fileupload

热点排行