从数据库中检索图像并在Asp.Net的网页中显示

时间:2014-11-19 11:23:39

标签: asp.net database image handler

我正在尝试将用户上传的个人资料图片存储在数据库中并从数据库中检索并显示在网页中。我已成功将图像保存在数据库中,但我不知道如何检索它。我试图显示但是当我点击检索按钮时没有任何反应。

这是Handler页面中的代码,而不是基于Id检索图像我试图通过传递将在欢迎页面上的用户名来实现。

     public void ProcessRequest(HttpContext context)
    {
       //passing name here
        if (context.Request.QueryString["name"] != null)
    {
        string csc = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
        using (SqlConnection con = new SqlConnection(csc))
        {
            SqlCommand com = new SqlCommand("RetrieveImage", con);
            com.CommandType = CommandType.StoredProcedure;
      //passing name
            SqlParameter p1 = new SqlParameter("@userName", context.Request.QueryString["name"]);
            com.Parameters.Add(p1);
            con.Open();
            SqlDataReader dr = com.ExecuteReader();
            dr.Read();
            context.Response.BinaryWrite((byte[])dr["image"]);
            dr.Close();
            con.Close();
        }
     }
         else
         {
             context.Response.Write("No Image Found");
         }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
     } 

我创建了一个存储过程,它接受用户名并给出了image.Stored过程名称是" RetrieveImage" 这是按钮点击事件。点击按钮" RetrieveImage"它调用处理程序并将值传递为" name"然后调用存储过程并显示图像

      protected void RetrieveImage_Click(object sender, EventArgs e)
        {
        string csc = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
        using (SqlConnection con = new SqlConnection(csc))
        {
            SqlCommand com = new SqlCommand("RetrieveImage", con);
            com.CommandType = CommandType.StoredProcedure;
            SqlParameter p1 = new SqlParameter("@userName", WelcomeUsername.Text);
            com.Parameters.Add(p1);
            con.Open();
            SqlDataReader dr = com.ExecuteReader();
        ImageDisp.ImageUrl = "~/ImagePage.ashx?name=" + WelcomeUsername.Text;
            con.Close();
        }
    }

当我运行页面时,我没有收到任何错误,但是当我点击按钮时没有任何反应。我是.Net的新手。请帮助。!!提前致谢

0 个答案:

没有答案
相关问题