从SQL数据表到asp的图像文件:图像控件

时间:2014-03-21 09:42:02

标签: c# asp.net sql image

我在使用SQL读取图像和在asp:Image控件中可视化时遇到一些问题。 场景是:

  1. 从本地计算机上传图片

  2. 网站将图片文件转换为位数组并保存

  3. 然后从表中读取它并将其解析为特定的asp:Image

  4. 我有以下代码:

    <asp:Content ID="wrapperContent" ContentPlaceHolderID="wrapper" Runat="Server">
    
        <asp:Image ID="profileImage" runat="server" ImageUrl="<%=img %>" /><br />
    
        <asp:Button ID="ChangeImage" runat="server" Text="Change Photo" OnClick="ChangeImage_Click" />
    
        <asp:FileUpload ID="FileUpload" runat="server" Visible="false" />
    
        <asp:Button ID="UploadImage" runat="server" Text="Upload Photo" visible="false" OnClick="UploadImage_Click"/>
    
    </asp:Content>
    

    代码背后:

    public partial class Details : System.Web.UI.Page
    {
        public int id;    
        public Bitmap bitmap;
        public string imgUrl;
    
    
        protected void Page_Load(object sender, EventArgs e)
        {   
    
            if (Session["userName"] != null)
            {
                string sql = "select * from users where username='" + Session["userName"]+"'";
                SqlDataReader sdr = operateData.getRow(sql);
                sdr.Read();
                id = Int32.Parse(sdr["Id"].ToString());
    
                sql = "select * from profiles where userId='" + id+"'";  
                SqlDataReader sdrPr = operateData.getRow(sql);
                sdrPr.Read();
    
            SqlConnection con = operateData.createCon();
                con.Open();
                SqlDataAdapter sda = new SqlDataAdapter(sql, con);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                DataRow row = dt.Rows[0];
                byte[] imgBytes = (byte[])row["img"];
                System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
    
                string filePath = Server.MapPath("temp") + "//" + "img" + DateTime.Now.Ticks.ToString() + ".png";
                FileStream fs = File.Create(filePath);
                fs.Write(imgBytes, 0, imgBytes.Length);
                fs.Flush();
                fs.Close();
    
                profileImage.ImageUrl = filePath;
    
            }
            else
            {
                Response.Redirect("Login.aspx");
            }       
        }
        protected void ChangeImage_Click(object sender, EventArgs e)
        {
            FileUpload.Visible = true;
            UploadImage.Visible = true;
        }
        protected void UploadImage_Click(object sender, EventArgs e)
        {
            if (FileUpload.HasFile)
            {
            string sql = "update profiles set img='" + img+"' where userId='" + id + "'";
                operateData.execSql(sql);            
            }
        }
    }
    

    我错过了什么..?我该怎么办呢?

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

请尝试以下代码。  在Web应用程序中,图像控制需要图像相对路径  更改图片网址如下

  

profileImage.ImageUrl =&#34; img&#34; + DateTime.Now.Ticks.ToString()+   &#34; png格式&#34 ;;

protected void Page_Load(object sender, EventArgs e)
        {   

            if (Session["userName"] != null)
            {
                string sql = "select * from users where username='" + Session["userName"]+"'";
                SqlDataReader sdr = operateData.getRow(sql);
                sdr.Read();
                id = Int32.Parse(sdr["Id"].ToString());

                sql = "select * from profiles where userId='" + id+"'";  
                SqlDataReader sdrPr = operateData.getRow(sql);
                sdrPr.Read();

            SqlConnection con = operateData.createCon();
                con.Open();
                SqlDataAdapter sda = new SqlDataAdapter(sql, con);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                DataRow row = dt.Rows[0];
                byte[] imgBytes = (byte[])row["img"];
                System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();

                string filePath = Server.MapPath("temp") + "//" + "img" + DateTime.Now.Ticks.ToString() + ".png";
                FileStream fs = File.Create(filePath);
                fs.Write(imgBytes, 0, imgBytes.Length);
                fs.Flush();
                fs.Close();

                profileImage.ImageUrl = "img" + DateTime.Now.Ticks.ToString() + ".png";

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