[英]asp.net display image(more than 2) from database using handler
Handler.ashx 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();
}
Exception Thrown from 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 Code Aspx代码
<asp:Image ID="Image2" runat="server" ImageUrl='<%#"Handler.ashx?ImID="+ Eval("PUserID")%>'
Height="115px" Width="115px" CssClass="img-border"/>
I want to display multiple images in data list 我想在数据列表中显示多个图像
Data List Bind 数据列表绑定
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();
}
Please Help me to display multiple images to datalist from database 请帮我从数据库中显示多个图像到数据列表
Try using Response.Clear();
尝试使用
Response.Clear();
at the begining of ProcessRequest function. 在ProcessRequest函数开始时。
Or 要么
you can use ApplicationInstance.CompleteRequest
instead of Response.Clear();
您可以使用
ApplicationInstance.CompleteRequest
代替Response.Clear();
Change your response content type from 从更改您的回复内容类型
context.Response.ContentType = "image";
To 至
context.Response.ContentType = "image/jpeg(png)"; depends on type
and remove context.Response.End()
并删除
context.Response.End()
Just see the below sample code 只看下面的示例代码
byte[] image = imagefromDB();
context.Response.OutputStream.Write(image, 0, image.Length);
context.Response.ContentType ="image-mime-type";
I can't assure Response.CLear(); 我不能保证Response.CLear(); is a good idea though.
不过是个好主意。 Sometimes it gets glitchy.
有时会出现故障。 I know, I've tried.
我知道,我已经尝试过了。
try using jpg with the image response instead of jpeg. 尝试将jpg和图片响应(而不是jpeg)一起使用。 Sometimes it doesn't work either.
有时它也不起作用。 jpg works all the time.
jpg一直有效。
When you use a Handler, each instance of where the handler is called has its own Response object. 使用处理程序时,调用处理程序的每个实例都有其自己的Response对象。 Therefore you can bind the dynamic paths of the images from the server code.
因此,您可以从服务器代码绑定图像的动态路径。
using a GridView and binding the RowDataBound event you can do this quite easily. 使用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")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.