使用通用处理程序 (ashx) 显示 base64 图像

Display base64 image using generic handler(ashx)

我正在尝试使用通用处理程序显示 base64,但没有成功。这是我的通用处理程序代码:

public class UserProfilePhoto : IHttpHandler, IReadOnlySessionState
{
        public void ProcessRequest(HttpContext context)
        {
            try
            {

                if (UserSession.Get().UserPhoto == null)
                {
                    context.Response.ContentType = "image/png";
                    context.Response.WriteFile("~/assets/img/avatar.png");
                }
                else
                {
                    context.Response.ContentType = UserSession.Get().UserPhoto.Substring(0, UserSession.Get().UserPhoto.IndexOf(","));
                    context.Response.Write(UserSession.Get().UserPhoto);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }

调用如下查看页面:

<img alt="Photo" ng-src="UserProfilePhoto.ashx" />

它没有更改 img 标签的 src。我不知道出了什么问题。帮助。 Here is the base64 string which I am getting from database.

实际上问题出在我的 base64 字符串中。它添加了内容类型。所以我删除了它,它使用 BinaryWrite 工作正常。

context.Response.ContentType = UserSession.Get().UserPhoto.Substring(0, UserSession.Get().UserPhoto.IndexOf(","));
var imageBytes = UserSession.Get().UserPhoto.Substring(UserSession.Get().UserPhoto.IndexOf(",")+1);
context.Response.BinaryWrite(Convert.FromBase64String(imageBytes));