VarBinary 到图像 url

2024-03-05

我正在将 Base64 图像转换为byte[]并将其存储在varbinarySQL Server 中的列。我想从数据库获取图像并将其设置为 ASP.NET 的图像 urlimage.

我怎样才能做到这一点?

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

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

尝试这样的事情

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

or

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

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

//resp is HttpResponseBase or just use Response.

resp.AddHeader("content-disposition", "attachment;filename=" + fileName);
resp.BinaryWrite(myByte);
resp.Flush();
resp.End();

Edit:这是 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
    }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

VarBinary 到图像 url 的相关文章

随机推荐