ASP.Net MVC:如何从模型显示字节数组图像 [英] ASP.Net MVC: How to display a byte array image from model
问题描述
我有一个带有字节数组图像文件的模型,我想在页面上显示它.
I've a model with a byte array image file that I want to show on the page.
如何在不返回数据库的情况下执行此操作?
How can I do that without going back to the Database?
我看到的所有解决方案都使用 ActionResult
返回数据库以检索图像,但我已经在模型上有了图像...
All the solutions that I see use an ActionResult
to go back to the database to retrieve the image, but I already have the image on the model...
推荐答案
这样的事情可能会奏效...
Something like this may work...
@{
var base64 = Convert.ToBase64String(Model.ByteArray);
var imgSrc = String.Format("data:image/gif;base64,{0}", base64);
}
<img src="@imgSrc" />
正如在下面的评论中所提到的,请使用上面的知识,虽然这可能会回答您的问题,但可能无法解决您的问题.根据您的问题,这可能是解决方案,但我不完全排除两次访问数据库的可能性.
As mentioned in the comments below, please use the above armed with the knowledge that although this may answer your question it may not solve your problem. Depending on your problem this may be the solution but I wouldn't completely rule out accessing the database twice.
这篇关于ASP.Net MVC:如何从模型显示字节数组图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!