VarBinary到图片的网址

VarBinary到图片的网址

问题描述:

我正在将base64图像转换为 byte [] ,并将其存储在SQL Server的 varbinary 列中.我想从数据库中获取图像,并将其设置为ASP.NET image 的图像url.

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.

我该怎么做?

用于将图像写入数据库的代码:

Code for writing image to database:

 string str= myBase64.Value.Substring(17);
 byte[] myByte = Convert.FromBase64String(str);

尝试类似的方法

<img src="GetBinary.aspx?id=123" />

<asp:Image ID="img" runat="server" ImageUrl="GetBinary.aspx?id=123" />

然后在GetBinary page_load中(如果是MVC,则使用返回null的操作).

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();

编辑:这是ASHX版本

<%@ 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
    }

}