ASP.NET MVC-base64String到html图像 [英] ASP.NET MVC - base64String to html image

查看:55
本文介绍了ASP.NET MVC-base64String到html图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个base64Strings集合,需要将其作为View上的图像查看.

I have a collection of base64Strings which need to be viewed as Images on a View.

有人知道我该怎么做吗?我完全迷住了.

Does anyone have an idea of how I can do this? I'm completely stumped.

推荐答案

您可以执行以下操作(这是针对一张图像的,但是您可以轻松地将其扩展为它们的列表).

You can do something like this (this is for one image, but you can easily extend it for a list of them).

(请参阅base64字符串的开头 " data:image/png; base64, ")

(See the header infront of the base64 string "data:image/png;base64,")

//Model
public class ImgModel
{
 public string Src { get; set; }
}

//Action
public ActionResult Index(int id = 0)
{
 var img =@"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==";
 return View(new ImgModel{Src = img});
}

//View
<div id="kk1">
 <img id="myimg1" src="@Model.Src" alt="Red dot">
</div>

我使用的示例img数据来自维基百科

The sample img data I have used is from Wikipedia

这篇关于ASP.NET MVC-base64String到html图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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