简体   繁体   English

从数据库中检索图像并将其设置为asp.net mvc5中的background-image属性css

[英]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. 我试图弄清楚如何设置上传到SQL Server的图片,并在div中将其设置为background-image样式。 I know that url function in css only accept the path to the image. 我知道CSS中的url函数仅接受图像的路径。 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>

You can create an action that returns image buffer like: 您可以创建一个返回图像缓冲区的操作,例如:

return File(imageBuffer, "image/jpeg");

and put action address in src attribute of img tag 并将动作地址放入img标签的src属性中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM