我是ASP.NET MVC Web开发的新手。
我想在SQL Server数据库的razor视图中以img标签显示图像。
在我的模型中,图像存储为字节数组:
public class Business
{
public string ID { get; set; }
[Required]
public string BusinessName { get; set; }
public string City { get; set; }
[Required]
public string Description { get; set; }
public byte[] LogoImage { get; set; }
}
在控制器I中从db [varbinary(MAX)]列获取图像数据:
...
LogoImage = (byte[])reader["LogoImage"]
...
在视图中,我有一个标准的详细信息模板,我在其中输入以下代码:
@{
var base64 = Convert.ToBase64String(model.LogoImage);
var imgSrc = String.Format("data:image/gif;base64,{0}", base64);
}
<img src='@imgSrc' style="max-width:100px; max-height:100px;" />
所以,我在剃刀视图中真的很挣扎,如果对待那个,那么另一个错误发生了。显示除图像外的其他属性。
有人可以建议如何在剃须刀视图中显示图像吗? 是否可以将图像填充为img src =&#39; ...长数据...&#39 ;? 如何从模型传递或访问数组?我是否必须将其转换为字符串@ Html.DisplayFor(model =&gt; model.LogoImage)?
答案 0 :(得分:1)
这里有两个选项:
@Html.Raw(imgSrc)
,因此它不会逃避base64字符串。如果你的形象很大,也许第一个选择更好。此外,所有浏览器可能都不支持数据网址。
答案 1 :(得分:0)
您可以通过此代码显示图像
<img style="max-width:100px; max-height:100px;" src="data:image/*;base64,@(Convert.ToBase64String(imgSrc))">