从ASP MVC数据库显示图片 [英] Display image from database in asp mvc
本文介绍了从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.
推荐答案
对于一个展会用语,使图像的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屋!
查看全文