I am converting a base64 image to byte[] and storing this in a varbinary column in SQL Server. I want to get the image from the database and set this as image url of an ASP.NET image.
How can I do that?
Code for writing image to database:
string str= myBase64.Value.Substring(17);
byte[] myByte = Convert.FromBase64String(str);
Try something like this
<img src="GetBinary.aspx?id=123" />
or
<asp:Image ID="img" runat="server" ImageUrl="GetBinary.aspx?id=123" />
then in GetBinary page_load (or if MVC, use an action that returns null).
//resp is HttpResponseBase or just use Response.
resp.AddHeader("content-disposition", "attachment;filename=" + fileName);
resp.BinaryWrite(myByte);
resp.Flush();
resp.End();
Edit: Here is an ASHX version
<%@ WebHandler Language="C#"
CodeBehind="AssetHandler.ashx.cs" Class="NameSpace.AssetHandler" %>
//-- codebehind
public class AssetHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
// place response code here using context.Response
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With