0

处理程序.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();
    }

请帮助我将多个图像显示到数据库中的数据列表

4

4 回答 4

0

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

使用 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")



         }
    }
于 2013-10-14T13:12:21.660 回答
0

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

或者

你可以使用ApplicationInstance.CompleteRequest而不是Response.Clear();

于 2013-10-14T13:03:58.773 回答
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"; 
于 2013-10-14T13:05:32.497 回答
0

我不能保证 Response.CLear(); 不过是个好主意。有时它会出现故障。我知道,我试过了。

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

于 2013-10-14T13:06:45.857 回答