如何渲染视图图像。图像值=字节[]。 asp.net的MVC [英] how to render image in view. value of image = byte[]. asp.net mvc
问题描述
我有类型的数据库映像值图像
。
鉴于下保存的类型字节[]
我想它显示在我的asp.net mvc的视图图像。最好的一样的形象。
如果图像是在 Model.ImageData保存的值。
i have image value in database of type "image"
.
in view saved under type byte[]
i want to show it as image in my asp.net mvc view. the best its just as image.
value if image is saved under Model.ImageData.
我试图解决这个问题是这样的:
i tried to solve it like this:
<img height="450px" width="330px" src="<%= historyItem.ImageData %>" alt="image" />
但它不工作。
怎么做?
but it doesnt work. how to do that?
推荐答案
你的方法的问题是,你已经包含了图像数据的视图模型的一部分,但在&LT; IMG&GT;
标记预计将在的src
属性的URL。有些浏览器支持的base64嵌入图像,从而HTML里面放,但我会建议你避免这种方法,因为您的标记的量可能会增长。
The problem with your approach is that you have included the image data as part of your view model but the <img>
tag expects an URL in the src
attribute. Some browsers support base64 embedded images to be put inside the HTML but I would recommend you to avoid this approach as the volume of your markup might grow.
所以,你需要一个控制器动作走秀图片:
So you will need a controller action which serves the image:
public ActionResult Image()
{
byte[] imageData = ...
return File(imageData, "image/png");
}
然后 IMG
标签的的src
属性指向这个控制器动作:
and then point the src
attribute of the img
tag to this controller action:
<img height="450px" width="330px" src="<%= Url.Action("Image") %>" alt="image" />
这篇关于如何渲染视图图像。图像值=字节[]。 asp.net的MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!