asp.net使用处理程序从数据库显示图像(超过2个)

时间:2013-10-14 12:58:12

标签: c# asp.net sql handler datalist

Handler.ashx

public void ProcessRequest (HttpContext context) 
{
        string imageid = context.Request.QueryString["ImID"];
        SqlConnection connection = new SqlConnection(con);
        connection.Open();
        SqlCommand command = new SqlCommand("SELECT PhotoStoreTB.Data FROM PhotoStoreTB INNER JOIN UserTB ON UserTB.UserID = PhotoStoreTB.UserID WHERE PhotoStoreTB.UserID ='" + imageid + "'", connection);
        SqlDataReader dr = command.ExecuteReader();
        dr.Read();
        byte[] imagedata = (byte[])dr[0];
        context.Response.ContentType = "image";

        using (System.IO.MemoryStream str = new System.IO.MemoryStream(imagedata, true))
        {
            str.Write(imagedata, 0, imagedata.Length);
            Byte[] bytes = str.ToArray();
            context.Response.BinaryWrite(bytes);
        }
        connection.Close();
        context.Response.End();
   }

context.Response.End();

引发的例外情况
{Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}

Aspx代码

<asp:Image ID="Image2" runat="server" ImageUrl='<%#"Handler.ashx?ImID="+ Eval("PUserID")%>'
                                                    Height="115px" Width="115px" CssClass="img-border"/>

我想在数据列表中显示多个图像

数据列表绑定

 try
    {
        ld.Openconnection();
        SqlCommand Cmd = new SqlCommand("PGetPropertyByCriteria", ld.con);
        Cmd.Parameters.AddWithValue("@StartIndex", 1);
        Cmd.Parameters.AddWithValue("@EndIndex", 10);

        Cmd.Parameters.AddWithValue("@flag", "Get");
        Cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter SqlAda = new SqlDataAdapter(Cmd);
        DataSet DsStudentDetails = new DataSet();
        SqlAda.Fill(DsStudentDetails);
        if (DsStudentDetails.Tables.Count > 0 && DsStudentDetails.Tables[0].Rows.Count > 0)
        {

            TotalPage = (Int32.Parse(DsStudentDetails.Tables[0].Rows[0]["row"].ToString()) / PageSize) + ((Int32.Parse(DsStudentDetails.Tables[0].Rows[0]["row"].ToString()) % PageSize) > 0 ? 1 : 0);

            CurrentRecord = DsStudentDetails.Tables[0].Rows.Count;
            DataTable tDataTable = new DataTable("PagingTable");
            tDataTable.Columns.Add(new DataColumn("LinkButtonVisible", typeof(bool)));
            tDataTable.Columns.Add(new DataColumn("DisplayName", typeof(string)));
            tDataTable.Columns.Add(new DataColumn("Value", typeof(string)));
            tDataTable.Columns.Add(new DataColumn("LabelVisible", typeof(bool)));


            dtlProduct.DataSource = DsStudentDetails.Tables[0];
            dtlProduct.DataBind();

        }
        else
        {
            DLPAGING.DataSource = null;
            DLPAGING.DataBind();
            dtlProduct.DataSource = null;
            dtlProduct.DataBind();
        }
    }
    catch (Exception ex)
    {
        ex.ToString();
    }
    finally
    {
        ld.Closeconnection();
    }

请帮我从数据库

datalist 显示多张图片

4 个答案:

答案 0 :(得分:0)

尝试在ProcessRequest函数开头使用Response.Clear();

您可以使用ApplicationInstance.CompleteRequest代替Response.Clear();

答案 1 :(得分:0)

更改您的回复内容类型
  context.Response.ContentType = "image";

  context.Response.ContentType = "image/jpeg(png)"; depends on type

删除 context.Response.End()

请参阅以下示例代码

        byte[] image = imagefromDB();            
        context.Response.OutputStream.Write(image, 0, image.Length); 
        context.Response.ContentType ="image-mime-type"; 

答案 2 :(得分:0)

我无法保证Response.CLear();虽然是一个好主意。有时它会变得很糟糕。我知道,我试过了。

尝试使用带有图像响应的jpg而不是jpeg。有时它也不起作用。 jpg一直都在工作。

答案 3 :(得分:0)

使用Handler时,调用处理程序的每个实例都有自己的Response对象。因此,您可以从服务器代码绑定图像的动态路径。

使用GridView并绑定RowDataBound事件,您可以非常轻松地完成此操作。

protected void gvAlbumImages_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           DataRow aRow = ((DataRowView)e.Row.DataItem).Row; 
           //This is the Row from your Datasource (which is a datatable)

          Image img =  (Image)e.Row[e.Row.RowIndex].FindControl("imgControl");
              //get the  Image object at the row you are binding to.
             //now simply set the source from the value in the DataRow
           img.ImageUrl = string.format("~/ImageHandler.ashx?ImageID={0}",aRow.Field<int>("ImageID")



         }
    }