在ASP MVC中显示数据库中的图像

我正在从控制器获取字节数组格式的图像,如何在视图中显示它?以最简单的方式。



波斯汪
浏览 620回答 3
3回答

BIG阳

创建一个用于使用Show操作显示图像的控制器,该操作采取要从数据库显示的图像ID。该操作应返回一个FileResult,其中包含具有适当内容类型的图像数据。public class ImageController : Controller{&nbsp; &nbsp; public ActionResult Show( int id )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var imageData = ...get bytes from database...&nbsp; &nbsp; &nbsp; &nbsp; return File( imageData, "image/jpg" );&nbsp; &nbsp; }}在您的视图中,构造图像,并使用图像ID使用控制器和操作为图像构造路径。<img src='<%= Url.Action( "show", "image", new { id = ViewData["imageID"] } ) %>' />
打开App,查看更多内容
随时随地看视频慕课网APP