Giter VIP home page Giter VIP logo

fsl.databaseimagebankinmvc's Introduction

FSL.DatabaseImageBankInMvc

Database Image Bank in MVC

The goal is use a database to store images and use MVC to call those images using custom routes. The URL must be something like that: "imagebank/sample-file" or "imagebank/32403404303".

enter image description here

LIVE DEMO:

http://codefinal.com/FSL.DatabaseImageBankInMvc/

FULL ARTICLE:

English: https://fabiosilvalima.net/en/database-image-bank-mvc/

Português: https://fabiosilvalima.net/banco-de-imagens-em-mvc/


What is in the source code?

FSL.DatabaseImageBankInMvc

  • Visual Studio solution file;
  • MVC and Web API application from .NET template;
  • Classes for our solution;

Remarks:

  • I created the application using the Web Application template and I have checked MVC template option. Visual Studio created a lot of files, views, scripts. I do not use them. Let's concentrate just the in our route and controller.

What is the goal?

The MVC Controller/Action will get the image by an ID "sample-file" or "32403404303" and find out on some cache and/or database and display the image. If exists in cache, get from cache if not get from database.

Assumptions:

  • If you want do not display the image and just download the file, use that: "imagebank/sample-file/download".

Source code...

Controllers/ImageBankController.cs

public ActionResult Index(string fileId, bool download = false)
{
            var defaultImageNotFound = "pixel.gif";
            var defaultImageNotFoundPath = $"~/content/img/{defaultImageNotFound}";
            var defaultImageContentType = "image/gif";

            var cacheKey = string.Format("imagebankfile_{0}", fileId);
            Models.ImageFile model = null;

            if (Cache.NotExists(cacheKey))
            {
                model = Repository.GetFile(fileId);

                if (model == null)
                {
                    if (download)
                    {
                        return File(Server.MapPath(defaultImageNotFoundPath), defaultImageContentType, defaultImageNotFound);
                    }

                    return File(Server.MapPath(defaultImageNotFoundPath), defaultImageContentType);
                }
                
                Cache.Insert(cacheKey, "Default", model);
            }
            else
            {
                model = Cache.Get(cacheKey) as Models.ImageFile;
            }

            if (download)
            {
                return File(model.Body, model.ContentType, string.Concat(fileId, model.Extension));
            }

            return File(model.Body, model.ContentType);
}
        
public ActionResult Download(string fileId)
{
	  return Index(fileId, true);
}

App_Start\RouteConfig.cs

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "ImageBank",
                url: GetImageBankRoute() + "/{fileId}/{action}",
                defaults: new { controller = "ImageBank", action = "Index" }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

Views/Home/Index.cshtml

<img src="~/imagebank/sample-file" />

References:

Licence:

  • [Licence MIT][4]

fsl.databaseimagebankinmvc's People

Watchers

James Cloos avatar Fabio Silva Lima avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.