在asp mvc中显示数据库中的图像 [英] Display image from database in asp mvc

查看:31
本文介绍了在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中显示数据库中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆