上传图片

 vs2003 图片上传,保存入数据库。添加与修改功能

如果存放图片文件夹不存在则创建修改上传另一张图片同时将以前图片删除

private   void  btnSub_Click( object  sender, System.EventArgs e)
        {
            
if (txtComName.Text == "" )
            {
                Utils.MessageBox(
" 请输入公司名称! " , this );
                
return ;
            }
            
string  address  =  txtAddr.Text;
            
string  tel  =  txtTel.Text;
            
string  comName  =  txtComName.Text;
            
string  introduct  =  txtIntroduct.Text;
            
string  website  =  txtWebSite.Text;
            
string  logo  =   "" ;
            
#region  图片上传
            
if (File1.PostedFile.FileName != string .Empty)
            {
                
string  fileFullName  =  File1.PostedFile.FileName;
                
string  strEx  =  fileFullName.Substring(fileFullName.LastIndexOf( " . " ) + 1 ).ToLower();
                
if (strEx == " jpg " || strEx == " gif " || strEx == " bmp " || strEx == " jpeg " )
                {
                    
string  datetime  =  DateTime.Now.ToString( " yyyyMMddhhmmss " );
                    
string  fileName  =   string .Format( " ../upLoad/CompanyImages/{0}.{1} " ,datetime,strEx);
                    
                    
// 保存之前 1 判断有没有文件夹
                     string  dir  =  Server.MapPath(fileName.Substring( 0 ,fileName.LastIndexOf( " / " )));
                    
if ( ! System.IO.Directory.Exists(dir))
                    {
                        System.IO.Directory.CreateDirectory(dir);
                    }
                    
//  2 删除以前的旧文件
                     if (txtLogo.Text != ""   &&  System.IO.File.Exists(txtLogo.Text))
                    {
                        System.IO.File.Delete(Server.MapPath(txtLogo.Text));
                    }
                    
                    
// 保存代码
                    File1.PostedFile.SaveAs(Server.MapPath(fileName));
                    logo 
=  fileName; // 存放在数据库里的路径
                }
                
else
                {
                    Utils.MessageBox(
" 图片类型不符! " , this );
                    
return ;
                }
            }
            
#endregion
            Company com 
=   new  Company();
            com.Addreaa 
=  address;
            com.CompanyName 
=  comName;
            com.Tel 
=  tel;
            com.Introduct 
=  introduct;
            com.Logo 
=  logo;
            com.Website 
=  website;

            
int  id  =  Utils.GetQueryInt( " id " );
            
if (id > 0 ) // 编辑
            {
                com.Company_ID 
=  id;
                
if (File1.PostedFile.FileName == string .Empty)
                {
                    logo 
=  txtLogo.Text;
                }
                
if (com.Update())                
                {
                    Utils.MessageBoxUrl(
" 修改成功!并返回 " , " CompanyList.aspx " );
                    txtAddr.Text 
=   "" ;
                    txtComName.Text 
=   "" ;
                    txtIntroduct.Text 
=   "" ;
                    txtLogo.Text 
=   "" ;
                    txtWebSite.Text
= "" ;
                    txtTel.Text
= "" ;
                }
                
else
                {
                    Utils.MessageBox(
" 修改失败! " );
                }
            }
            
else
            {
                com.Add();
                
if (com.Company_ID > 0 )
                {
                    Utils.MessageBox(
" 添加成功!并返回 " , " CompanyList.aspx " );
                    txtAddr.Text 
=   "" ;
                    txtComName.Text 
=   "" ;
                    txtIntroduct.Text 
=   "" ;
                    txtLogo.Text 
=   "" ;
                    txtWebSite.Text
= "" ;
                    txtTel.Text
= "" ;
                }
                
else
                {
                    Utils.MessageBox(
" 添加失败! " );
                }
            }

        }

 

vs2003 图片上传 没有存数据库

Code

 

vs2005 图片上传并存储在数据库中

Code

你可能感兴趣的:(上传)