以二进制码形式将图片存到数据库中,再从数据库中以二进制的形式从数据库中读出来

1.FileUpload控件和上传的button控件

    <asp:FileUpload ID="fuPhoto" runat="server" Style="width: 150px;" />
    <asp:Button ID="btnSaveImage" runat="server" Text="上传" OnClick="btnSaveImage_Click"  Style="width: 50px;" ValidationGroup="QUXIAO" />

2.button的点击事件

 protected void btnSaveImage_Click(object sender, EventArgs e)
        {
            HttpPostedFile UpFile = fuPhoto.PostedFile;
            try
            {
                if (UpFile.ContentLength == 0)
                {      //文件长度为零时 
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择要上传的图片')</script>");
                    //Page.RegisterStartupScript("err", "<script language=javascript>alert('请你选择你要上传的文件');</script>");
                }
                else
                {
                    FileByteArray = new Byte[UpFile.ContentLength];      //图象文件临时储存Byte数组 
                    Stream StreamObject = UpFile.InputStream;    //建立数据流对像 
                    //读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度 
                    StreamObject.Read(FileByteArray, 0, UpFile.ContentLength);
                    Session["photo"] = FileByteArray;
                    this.Image1.ImageUrl = "ImageView.aspx?random=" + new Random().Next().ToString();

                }
            }
            catch (Exception err)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传文件失败')</script>");
                //Page.RegisterStartupScript("err", "<script language=javascript>alert('上传文件失败," + err.Message + "');</script>");
            }

 

        }

 

3.建一个新页面(ImageView.aspx.)

此页面前台不放任何控件,后台代码:

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["photo"] != null)
                {
                    Byte[] FileByteArray = (Byte[])Session["photo"];
                    Response.BinaryWrite(FileByteArray);
                    Response.End();
                }
                else
                {
                    string name = HttpContext.Current.Server.MapPath("~/images/nothing.jpg");
                    FileStream fs = File.OpenRead(name);
                    byte[] content = new byte[fs.Length];
                    fs.Read(content, 0, content.Length);
                    Response.BinaryWrite(content);
                    Response.End();
                }
            }


        }

4.在上传页面的pageload写上如代码.

         if (!IsPostBack)
            {
                Image1.ImageUrl = "ImageView.aspx?random=" + new Random().Next().ToString();
            }

5.将上传的图片以二进制的形式存到数据库中

pageload中  FileByteArray = null;Session["photo"] = null;

保存方法中  patInfo.Photo = (Byte[])Session["photo"];

6.将二进制的图片从数据库中读出来

公共方法中

  public string saveFilePath
        {
            get { return ViewState["saveFilePath"].ToString(); }
            set { ViewState["saveFilePath"] = value; }
        }
        public Byte[] FileByteArray
        {
            get
            {

                return ViewState["FileByteArray"] as Byte[];

            }

            set { ViewState["FileByteArray"] = value; }


        }

显示图片

                    FileByteArray = (Byte[])patInfo.Photo;
                    Session["photo"] = (Byte[])patInfo.Photo;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值