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

时间:2021-11-29 23:10:00

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

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;