我正在尝试使用通用处理程序显示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.
答案 0 :(得分:1)
实际上问题出在我的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));