0

I store images in the database and can retrieve them in a view thanks to a Get Method from my Img Controller that returns a File([] fileContents, string contentType).

No problem to display it in a View with :

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

On the contrary, when I try to pass the URL of an image within an inline background-image: url, it does not work.

1) I tried this in the View using the same Img Controller to get the image :

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

Which renders this HTML but does not display the image :

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

2) I tried this in the User Controller :

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;

And this in the View :

<div class="img" style="background-image: url('@ViewBag.ImgSrc')"></div>

which renders this HTML but does not display the image :

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

I don't understand... have you any idea?

I know someone had the same problem but he did not share how he finally managed to solve it : ASP MVC: Upload Image from Database as Full Background CSS

Thanks in advance

1
  • thanks Mate, that was my fault... Commented Sep 22, 2019 at 16:54

1 Answer 1

0

I am such an idiot !

I was so focused on the background-image: url ('...') because the color syntax of visual studio would not get right that I completely forgot to give a size to that damn div !

Both methods are actually working well and displaying the image.

Sorry about that

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.