# Store Images in a Database using the ImageBrowser of the Editor

## Environment

<table>
 <tr>
  <td>Product</td>
  <td> Telerik UI for ASP.NET MVC Editor</td>
 </tr>
 <tr>
  <td>Progress Telerik UI for ASP.NET MVC version</td>
  <td>2024.4.1112</td>
 </tr>
</table>

## Description

How can I use the Editor's ImageBrowser with a database?

## Solution

See the full example on how to [set up the Editor `ImageBrowser` to store the images in an MS SQL database](https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Areas/EditorDatabaseImageBrowser) in an ASP.NET MVC application.

1. Implement the `Upload` action, to which the ImageBrowser uploads the selected files.

   ```C# HomeController
       public ActionResult Upload(string path, HttpPostedFileBase file)
       {
           if (file != null)
           {
               var files = new FilesRepository();
               var parentFolder = files.GetFolderByPath(path);
               if (parentFolder != null)
               {
                   files.SaveImage(parentFolder, file);
                   return Json(
                       new FileBrowserEntry
                       {
                           Name = Path.GetFileName(file.FileName),
                           Size = file.ContentLength
                       }
                   , "text/plain");
               }
           }
           throw new HttpException(404, "File Not Found");
       }
   ```

2. Implement the `FileRepository` class, which performs the data operations with the database.

   ```C# FilesRepository.cs
       using System;
       using System.Collections.Generic;
       using System.IO;
       using System.Linq;
       using System.Web;
       using Kendo.Mvc.UI;

       public class FilesRepository
       {
           private ImageBrowserEntities dataContext;

           protected ImageBrowserEntities Db
           {
               get { return dataContext ?? (dataContext = new ImageBrowserEntities()); }
           }

           public IEnumerable<FileBrowserEntry> Images(string path)
           {
               return Images(GetFolderByPath(path));
           }

           public void SaveImage(Folder parent, HttpPostedFileBase file)
           {
               var buffer = new byte[file.InputStream.Length];
               file.InputStream.Read(buffer, 0, (int) file.InputStream.Length);
               var image = new Image
                           {
                               Name = Path.GetFileName(file.FileName),
                               Folder = parent,
                               Image1 = buffer
                           };
               Db.Images.Add(image);
               Db.SaveChanges();
           }

           //... additional methods...
       }
   ```

## More ASP.NET MVC Editor Resources

* [ASP.NET MVC Editor Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/editor/overview)

* [ASP.NET MVC Editor Demos](https://demos.telerik.com/aspnet-mvc/editor/index)

* [ASP.NET MVC Editor Product Page](https://www.telerik.com/aspnet-mvc/editor)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Client-Side API Reference of the Editor for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/editor)

* [Server-Side API Reference of the Editor for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/editor)

* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2024-backwards-compatibility)

* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
