Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VarBinary to image url

Tags:

c#

asp.net

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);
like image 608
JIKKU Avatar asked Jun 23 '26 20:06

JIKKU


1 Answers

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
    }

}
like image 150
Valamas Avatar answered Jun 25 '26 14:06

Valamas