[英]How to show my image from database on an ASP.NET Core MVC page?
I'm Ali I need some help.我是阿里,我需要一些帮助。 I need to display an image in a view.
我需要在视图中显示图像。
I can to store the image in the database by converting it to an array and storign it.我可以通过将图像转换为数组并存储它来将图像存储在数据库中。 But now I can't display the image in my view.
但现在我无法在我的视图中显示图像。
How do that?怎么做?
I would like to support you with an example.我想举个例子来支持你。
My model class:我的 model class:
One of the cleanest way to do this is to create a new HtmlExtension class which will convert bytes to base64 string and then returns the mvc html string最干净的方法之一是创建一个新的 HtmlExtension class 它将字节转换为 base64 字符串,然后返回 mvc html 字符串
public static class HtmlExtensions
{
public static MvcHtmlString Image(this HtmlHelper html, byte[] image)
{
var img = String.Format("data:image/jpg;base64,{0}", Convert.ToBase64String(image));
return new MvcHtmlString("<img src='" + img + "' />");
}
}
Then you can use that in any view然后你可以在任何视图中使用它
@Html.Image(Model.Imag)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.