c# - Retrieve image from database and set it to background-image property css in asp.net mvc5
问题描述
I'm trying to figure out how I can set the picture that I uploaded to SQL Server, and set it to background-image
style in div. I know that url function in css only accept the path to the image. Is there any workaround that can config for the property to accept the image file directly from database?
<div class="item-2">
<a href="@Url.Action("Details", new { id = item.id })" class="card">
@{
var base64 = Convert.ToBase64String(item.img);
var imgsrc = string.Format("data:image/jpg;base64,{0}", base64);
<div class="thumb" style="background-image: url('@imgsrc');"></div>
<article>
<h1>@Html.DisplayFor(modelItem => item.name)</h1>
<p>@Html.DisplayFor(modelItem => item.info)</p>
<span>Major</span>
</article>
}
</a>
</div>
解决方案
您可以创建一个返回图像缓冲区的操作,例如:
return File(imageBuffer, "image/jpeg");
并将动作地址放在 img 标签的 src 属性中
推荐阅读
- javascript - 如何从角度 FormControl 获取数据属性
- android - 配置 CameraX(闪光模式、自动白平衡模式、动作模式等)
- ruby-on-rails - 错误:参数数量错误(给定 0,预期 2)
- html - 可以在css中存储键值对吗?
- python - 无法使用 yml 文件在 conda 中创建环境
- oracle - 如何在 oracle pl/sql 中的 where 或 join 子句中使用 50,000 个 ID 进行选择查询?
- c# - 如何在客户端机器上运行水晶报表而不给出完整路径
- excel - Excel - 根据来自不同列的值过滤列
- google-bigquery - Bigquery 中的日期范围分区
- function - Octave 并行包:用户定义的函数调用问题