1

I'm loading an image from database and showing this image in the _Layout.cshtml.

The image can change (after log in) and each company may have a different image, so after the log in, I go to the database and get the corresponded image for that company (based on it's Id). I need to keep this loaded image and is not needful to go to the server again.

Everything work fine, I can load the image and show in the _Layout, but after every request, the Image "blinks" because the _Layout is being recreated.

I've followed this question (step 1) to load the image from database.

The code that I have is:

    public ActionResult CarregarLogoEmpresa()
    {
        if (SecurityManager.Instance.Identity.IsAuthenticated)
        {
            var codigoEmpresaLogada = SecurityManager.Instance.Session.IdEmpresa;

            var logoEmpresa = _empresaAppService.BuscarLogoEmpresa(codigoEmpresaLogada);

            //return Json(Convert.ToBase64String(logoEmpresa), JsonRequestBehavior.AllowGet);

            return File(logoEmpresa, "image/jpg");
        }

        return null;
    }

And in the View:

<img src='@Url.Action("CarregarLogoEmpresa", "Image")' />

Is there a way keep the Image loaded instead of being recreated, avoiding the blinking everytime?

4
  • 1
    Could you post the code you have please? Commented Dec 12, 2016 at 10:49
  • @antoinedelia ready! Commented Dec 12, 2016 at 11:00
  • Placing the image outside of the DB would probably allow the browsers cache to not "reload" it. Or have a look at MVC's partialviews, so that you don't have to reload the layout each request. Commented Dec 12, 2016 at 11:06
  • @Mackan, I made some tests here and I could sort it out with [OutputCache] in the Action. Since the Image must be loaded once (and will not modify) Commented Dec 12, 2016 at 11:34

3 Answers 3

3

This blink probably occurs because of the time it takes to load the image from the database. I recommend using Asp.Net OutputCache in order to minimize this load time.

Action:

[OutputCahce(VaryByParam="CodigoEmpresaLogada", Duration=30)]
public ActionResult CarregarLogoEmpresa(int CodigoEmpresaLogada)
{
    if (SecurityManager.Instance.Identity.IsAuthenticated)
    {
        var logoEmpresa = _empresaAppService.BuscarLogoEmpresa(CodigoEmpresaLogada);

        //return Json(Convert.ToBase64String(logoEmpresa), JsonRequestBehavior.AllowGet);

        return File(logoEmpresa, "image/jpg");
    }

    return null;
}

View:

<img src='@Url.Action("CarregarLogoEmpresa", "Image", new {CodigoEmpresaLogada = SecurityManager.Instance.Session.IdEmpresa})' />
Sign up to request clarification or add additional context in comments.

Comments

1

Your image will blink on every request regardless of it being loaded from the database or not. This effect may be amplified because of the time it takes to pull the image out of the database every time. I would advise you to consider another option, if you have one. If you're using Azure or AWS, for instance, there are file/blob storages you can use to host the image that might be faster than loading it from the database. I particularly don't like storing images on databases because it's expensive and the backups get bigger.

Going back to the "blink" problem. Some sites like GitHub won't reload the "layout" on every request, just the content that changed. The standard library for doing that is pjax (I believe GitHub uses pjax). Even though GitHub is made on Rails, the solution should work fine for ASP.NET MVC too.

Take a look:

4 Comments

That's a good point to analyze. Definitely I'll take a look in those examples. For now, doing some tests, I could sort it out with [OutputCache], I don't know if it's the better approach of doing this as well.
please correct or better word.. "Your image will blink on every request regardless of it being loaded from the database or not." that is not true. the reason its "blinking" is because the image is pulled every-time from the server and comes from loading the page. having the image as a resource i.e static content... will allow the client browser/pc to cache the image... which means no more "blinking" on page load. The suggestion here should be that if its part of the layout then the image should not be loaded from db or mvc controller and should be normal web content.
@Seabizkit, the image is dynamic loaded from the server. It won't be the same for the same people I mean, if someone log in, will display an Image for him, if another person log in, another image will be displayed. The point is that once this image is loaded, it must be the same during the process. By the way, I could sort it out using [OutputCache] from MVC!
@Maturano i get what your saying.. personal i would have folder structure to support this, create them dynamically and just save the relative path, and then just fetch the path from the DB
0

After some researchs, I could sort it out using the Asp.Net MVC ActionFilter

[OutputCache(Duration = 300, VaryByParam="none", Location = OutputCacheLocation.Client)]

on the ActionResult.

I don't know if it's the better approach, but for this case, at least, worked for me.

1 Comment

The key is the location of cache. Other option is to pass the CodigoEmpresaLogada and store the cache in Any place.

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.