简体   繁体   English

如何在 ASP.NET Core MVC 页面上显示来自数据库的图像?

[英]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:

enter image description here在此处输入图像描述

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.

相关问题 如何在 ASP.NET Core MVC 中显示数据库中的图像? - How to display an image from the database in ASP.NET Core MVC? 如何将数据库逻辑从我的 Asp.Net MVC 应用程序转移到 ASP.Net Core MVC? - How can I transfer the database logic from my Asp.Net MVC Apllication to ASP.Net Core MVC? 如何从数据库下载文件,并在 Asp.net core MVC 中将它们作为图像获取? - How to download files from a database, and get them as image in Asp.net core MVC? 在控制台应用程序中重用来自我的 ASP.NET Core MVC 应用程序的代码:如何进行数据库连接 - Reusing code from my ASP.NET Core MVC app in console app : how to do database connection 如果数据库 ASP.NET CORE 3.1 中不存在图像,如何显示显示默认图像 - How to show a show default image if image not exist in database ASP.NET CORE 3.1 上传图片并显示在 asp.net 核心 mvc - upload image and show that in asp.net core mvc 我想在 ASP.NET Core MVC 上显示视频 SQL 数据库视图 - I want to Show Video on ASP.NET Core MVC View from SQL Database ASP.NET Core MVC,从数据库中获取文件,并呈现为图像 - ASP.NET Core MVC, Get file from database, and render as image 无法从 asp.net 核心 mvc 中的数据库更新我的 model - Unable to update my model from database in asp.net core mvc 使用ASP.NET MVC Core将数据从数据库注入到类中 - Inject data from database to a class with ASP.NET MVC Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM