在asp mvc中显示数据库中的图像

编程入门 行业动态 更新时间:2024-10-25 12:19:06
本文介绍了在asp mvc中显示数据库中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I'm getting an image in a byte array format from the controller, How can I display this in the view? in the simplest way.

推荐答案

使用 Show 操作创建一个用于显示图像的控制器,该操作从数据库中获取要显示的图像的 id.该操作应返回包含具有适当内容类型的图像数据的 FileResult.

Create a controller for displaying images with a Show action that takes the id of the image to display from the database. The action should return a FileResult that contains the image data with the appropriate content type.

public class ImageController : Controller { public ActionResult Show( int id ) { var imageData = ...get bytes from database... return File( imageData, "image/jpg" ); } }

在您的视图中,构建图像并使用图像 id 使用控制器和动作为图像构建路径.

In your view, construct the image and use the image id to construct a path for the image using the controller and action.

<img src='<%= Url.Action( "show", "image", new { id = ViewData["imageID"] } ) %>' />

更多推荐

在asp mvc中显示数据库中的图像

本文发布于:2023-11-27 20:19:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639461.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   图像   asp   mvc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!