【问题标题】:Asp Mvc : set inline css background-image:url with an image stored in the databaseAsp Mvc:使用存储在数据库中的图像设置内联 css background-image:url
【发布时间】:2019-09-22 15:48:52
【问题描述】:

我将图像存储在数据库中,并且可以在视图中检索它们,这要归功于我的 Img 控制器中的 Get 方法,该方法返回 File([] fileContents, string contentType)

在视图中显示它没问题:

<img src="@Url.Action("Get", "Img", new { id = Model.ImgId})"

相反,当我尝试通过内联 background-image: url 传递图像的 URL 时,它不起作用。

1) 我在视图中尝试使用相同的 Img 控制器来获取图像:

<div style="background-image: url('@Url.Action("Get", "Img", new { id = Model.ImgId})')"></div>

呈现此 HTML 但不显示图像:

<div class="imageBox" style="background-image: url('/Img/Get/2')"></div>

2) 我在用户控制器中试过这个:

string formatWithoutPoint = user.Img.Format.Substring(1); string imageBase64Data = Convert.ToBase64String(user.Img.Data); string imageDataURL = string.Format("data:image/" + formatWithoutPoint + ";base64,{0}", imageBase64Data); ViewBag.ImgSrc = imageDataURL;

这在视图中:

&lt;div class="img" style="background-image: url('@ViewBag.ImgSrc')"&gt;&lt;/div&gt;

呈现此 HTML 但不显示图像:

<div style="background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAU...')"></div>

我不明白...你知道吗?

我知道有人遇到了同样的问题,但他没有分享他最终是如何解决的:ASP MVC: Upload Image from Database as Full Background CSS

提前致谢

【问题讨论】:

  • 谢谢队友,那是我的错……

标签: asp.net-mvc


【解决方案1】:

我真是个白痴!

我非常专注于 background-image: url ('...') 因为 Visual Studio 的颜色语法不正确,以至于我完全忘记了给那个该死的 div 一个大小!

这两种方法实际上都运行良好并显示图像。

对不起

【讨论】:

    猜你喜欢
    • 2019-04-30
    • 1970-01-01
    • 2011-03-11
    • 2012-12-05
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    相关资源
    最近更新 更多