从Byte [] Asp.Net MVC 3加载Image Src [英] Load Image Src from Byte[] Asp.Net MVC 3
问题描述
我有一个ASP.net MVC 3应用程序,我想在模型中将图像存储为字节,然后将其从模型加载到html图像标记的src属性中。
I have an ASP.net MVC 3 application where I want to store an image as bytes in the model and then load it from the model into the src attribute of an html image tag.
例如
//Property in Model
public byte[] Image { get; set; }
但是当我尝试这个时它会出错:
But when I try this it errors:
<img src = "@Model.Image" alt=""/>
如何从字节加载图像?我想避免再次调用Controller将图像作为FileResult。
How can I load the image from bytes? I want to avoid calling the Controller again to get the image as a FileResult.
是否可以?
推荐答案
最简单的方法是这样的:
The easiest way is something like this:
<img src="data:image/png;base64,@System.Convert.ToBase64String(Model.Image)" alt=""/>
这假设是PNG有效负载,并且不受传统浏览器的很好支持。
This assumes a PNG payload and is not very well supported by legacy browsers.
我实际上建议将其保存到磁盘并让你的网络服务器单独托管它。
I would actually recommend saving it to disk and letting your web server host it up separately.
这篇关于从Byte [] Asp.Net MVC 3加载Image Src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!