上传图片至数据库

来源:互联网 发布:我知这世界本如 知乎 编辑:程序博客网 时间:2024/04/28 00:39
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class addtask : System.Web.UI.Page
{
    
static int id;
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (Session["id"== null)
        
{
            Response.Redirect(
"Default.aspx");
        }

        
else
        
{
            
if (Session["IsMain"].ToString() == "1")
            
{

            }

            
else
            
{
                
if (Session["IsMain"].ToString() == "2")
                
{
                }

                
else
                
{
                    Response.Redirect(
"Default.aspx");
                }

            }

        }


        
if (!this.IsPostBack)
        
{
            id 
= Convert.ToInt32(Request.QueryString["id"]);
            DataSet ds 
= Custmer.SelCustmer(id);
            
this.Label15.Text = ds.Tables[0].Rows[0][1].ToString();
            
this.Label16.Text = ds.Tables[0].Rows[0][2].ToString();
            ds.Clear();
            
this.Panel1.Visible = true;
            
this.Panel2.Visible = false;
            
this.Panel3.Visible = false;
            
this.Badd.Attributes["onclick"= "javascript:jump();";

        }

    }


    
protected void Badd_Click(object sender, EventArgs e)
    
{
        DataSet ds 
= Custmer.SelCustmer(id);
        
int iOperatorid = Convert.ToInt32(ds.Tables[0].Rows[0][19]);
        
byte strType;
        
if (RadioButton1.Checked)
        
{
            strType 
= 2;
        }

        
else
        
{
            strType 
= 1;
        }

        
string strExplain = this.TextBox2.Text;

        
/////////////////////////图片byte//////////////////////
        Stream file;
        
string picType;
        
int iLenght;
        
byte[] fileByte;
        
if (FUupdate.PostedFile.FileName != "")
        
{
            picType 
= FUupdate.PostedFile.ContentType;
            
string fillname = FUupdate.PostedFile.FileName;
            
string filetype = fillname.Substring(fillname.LastIndexOf("."+ 1);
            
if (filetype == "gif" || filetype == "jpg")
            
{
                file 
= FUupdate.PostedFile.InputStream;
                iLenght 
= FUupdate.PostedFile.ContentLength;
                fileByte 
= new byte[iLenght];
                file.Read(fileByte, 
0, iLenght);
                
if (Service.AddService(id, iOperatorid, picType, fileByte, strType, strExplain))
                
{
                    
this.Panel1.Visible = false;
                    
this.Panel3.Visible = true;
                }

                
else
                
{
                    
this.Panel1.Visible = false;
                    
this.Panel2.Visible = true;
                }

            }

            
else
            
{
                
this.Label5.Text = "文件格式不对!";
            }

        }

        
else
        
{
            picType 
= null;
            fileByte 
= null;
            
if (Service.AddService(id, iOperatorid, strType, strExplain))
            
{
                
this.Panel1.Visible = false;
                
this.Panel3.Visible = true;
            }

            
else
            
{
                
this.Panel1.Visible = false;
                
this.Panel2.Visible = true;
            }

        }

    }

}