Rendering bytes from sql server to an image control?
Markup:
<asp:ListView ID="lvGallery" runat="server" DataSourceID="SqlDataSource1">
<LayoutTemplate>
<table runat="server" id="tableGallery">
<tr runat="server" id="itemPlaceHolder"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<div class="box_img2">
<div class="g_size">
<a runat="server" id="linkImage">
<img id="Image1" runat="server" src="MyImage.jpg" />
</a>
</div>
</div>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="..." ProviderName="System.Data.SqlClient" SelectCommand="SELECT [Image] FROM [GalleryImages]"></asp:SqlDataSource>
Code-behind:
public void ProcessRequest(HttpContext context)
{
//write your handler implementation here.
string username = Convert.ToString(context.Request.QueryString["username"]);
if (username != null)
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
byte[] arrContent;
DataRow dr;
string strSql;
strSql = "Select Image from GalleryImages where username = '" + username + "'";
da = new SqlDataAdapter(strSql, connection.ConnectionString);
da.Fill(ds);
dr = ds.Tables[0].Rows[0];
arrContent = (byte[])dr["ImageFile"];
开发者_如何学编程 context.Response.ContentType = "jpeg";
context.Response.OutputStream.Write(arrContent, 0, arrContent.Length);
context.Response.End();
}
}
I have also added httphandlers section in web.config. But I don't get the images in ListView control.
You have to create an http handler that returns the image
public void ProcessRequest(HttpContext context)
{
byte[] yourImage = //get your image byte array
context.Response.BinaryWrite(yourImage);
context.Request.ContentType = "image/jpeg";
context.Response.AddHeader("Content-Type", "image/jpeg");
context.Response.AddHeader("Content-Length", (yourImage).LongLength.ToString());
con.Close();
context.Response.End();
context.Response.Close();
}
You can can do that by creating a GenericHandler file type from the visual studio and add the previous code in then you can call you can write the url of the generic handler as the image source
See full article here.
public class NWEmpPhotoHandler : IHttpHandler
{
public bool IsReusable { get { return true; } }
public void ProcessRequest(HttpContext ctx)
{
string id = ctx.Request.QueryString["id"];
SqlConnection con = new SqlConnection(<<INSERT CONNECTION STRING HERE>>);
SqlCommand cmd = new SqlCommand("SELECT Photo FROM Employees WHERE EmployeeID = @EmpID", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@EmpID", id);
con.Open();
byte[] pict = (byte[])cmd.ExecuteScalar();
con.Close();
ctx.Response.ContentType = "image/bmp";
ctx.Response.OutputStream.Write(pict, 78, pict.Length - 78);
}
}
Cool question. I would write a class implementing IHttpHandler
which responds to requests for the image file(s).
See my answer in the following question for an example of how to get file streams in and out of a SQL varbinary(max) field.
How to make a fileupload interface in ASP.NET
Don't forget to use the correct MIME type when crafting the HTTP response.
Download and Upload images from SQL Server via ASP.Net MVC.
The code is MVC but the idea behind the implementation is not MVC specific and can be used in generic ASP. The gist of the issue is that you want to avoid reading the database BLOB into a byte array and then writing the byte array into the response, doing so will result in poor performance under load due to the many large size array allocations (an image can easily go into 2 MB+ and large images can go into tens and even hundreds of MB). This article shows how to do a stream based implementation where the bytes are sent back to the HTTP response as they are read from the database.
精彩评论