|
| 1 | +--- |
| 2 | +title: Store Images in a Database using the ImageBrowser of the Editor |
| 3 | +description: Set up the ImageBrowser of the Telerik UI for {{ site.framework }} Editor to store images in an MS SQL database. |
| 4 | +type: how-to |
| 5 | +page_title: Store Images in a Database using the ImageBrowser of the Editor |
| 6 | +slug: editor-imagebrowser-with-database |
| 7 | +tags: editor, image, store, database, sql, browser, core, mvc, telerik |
| 8 | +previous_url: /helpers/editors/editor/how-to/database-with-imagebrowser, /html-helpers/editors/editor/how-to/database-with-imagebrowser |
| 9 | +res_type: kb |
| 10 | +component: editor |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +<table> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td> {{ site.product }} Editor</td> |
| 19 | + </tr> |
| 20 | + <tr> |
| 21 | + <td>Progress {{ site.product }} version</td> |
| 22 | + <td>2024.4.1112</td> |
| 23 | + </tr> |
| 24 | +</table> |
| 25 | + |
| 26 | +## Description |
| 27 | + |
| 28 | +How can I use the Editor's ImageBrowser with a database? |
| 29 | + |
| 30 | +## Solution |
| 31 | + |
| 32 | +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. |
| 33 | + |
| 34 | +1. Implement the `Upload` action, to which the ImageBrowser uploads the selected files. |
| 35 | + |
| 36 | + ```HomeController |
| 37 | + public ActionResult Upload(string path, HttpPostedFileBase file) |
| 38 | + { |
| 39 | + if (file != null) |
| 40 | + { |
| 41 | + var files = new FilesRepository(); |
| 42 | + var parentFolder = files.GetFolderByPath(path); |
| 43 | + if (parentFolder != null) |
| 44 | + { |
| 45 | + files.SaveImage(parentFolder, file); |
| 46 | + return Json( |
| 47 | + new FileBrowserEntry |
| 48 | + { |
| 49 | + Name = Path.GetFileName(file.FileName), |
| 50 | + Size = file.ContentLength |
| 51 | + } |
| 52 | + , "text/plain"); |
| 53 | + } |
| 54 | + } |
| 55 | + throw new HttpException(404, "File Not Found"); |
| 56 | + } |
| 57 | + ``` |
| 58 | +
|
| 59 | +1. Implement the `FileRepository` class, which performs the data operations with the database. |
| 60 | +
|
| 61 | + ```FilesRepository.cs |
| 62 | + using System; |
| 63 | + using System.Collections.Generic; |
| 64 | + using System.IO; |
| 65 | + using System.Linq; |
| 66 | + using System.Web; |
| 67 | + using Kendo.Mvc.UI; |
| 68 | +
|
| 69 | + public class FilesRepository |
| 70 | + { |
| 71 | + private ImageBrowserEntities dataContext; |
| 72 | +
|
| 73 | + protected ImageBrowserEntities Db |
| 74 | + { |
| 75 | + get { return dataContext ?? (dataContext = new ImageBrowserEntities()); } |
| 76 | + } |
| 77 | +
|
| 78 | + public IEnumerable<FileBrowserEntry> Images(string path) |
| 79 | + { |
| 80 | + return Images(GetFolderByPath(path)); |
| 81 | + } |
| 82 | +
|
| 83 | + public void SaveImage(Folder parent, HttpPostedFileBase file) |
| 84 | + { |
| 85 | + var buffer = new byte[file.InputStream.Length]; |
| 86 | + file.InputStream.Read(buffer, 0, (int) file.InputStream.Length); |
| 87 | + var image = new Image |
| 88 | + { |
| 89 | + Name = Path.GetFileName(file.FileName), |
| 90 | + Folder = parent, |
| 91 | + Image1 = buffer |
| 92 | + }; |
| 93 | + Db.Images.Add(image); |
| 94 | + Db.SaveChanges(); |
| 95 | + } |
| 96 | +
|
| 97 | + //... additional methods... |
| 98 | + } |
| 99 | + ``` |
| 100 | +
|
| 101 | +## More {{ site.framework }} Editor Resources |
| 102 | +
|
| 103 | +* [{{ site.framework }} Editor Documentation]({%slug htmlhelpers_editor_aspnetcore%}) |
| 104 | +
|
| 105 | +* [{{ site.framework }} Editor Demos](https://demos.telerik.com/{{ site.platform }}/editor/index) |
| 106 | +
|
| 107 | +{% if site.core %} |
| 108 | +* [{{ site.framework }} Editor Product Page](https://www.telerik.com/aspnet-core-ui/editor) |
| 109 | +
|
| 110 | +* [Telerik UI for {{ site.framework }} Video Onboarding Course (Free for trial users and license holders)]({%slug virtualclass_uiforcore%}) |
| 111 | +
|
| 112 | +* [Telerik UI for {{ site.framework }} Forums](https://www.telerik.com/forums/aspnet-core-ui) |
| 113 | +
|
| 114 | +{% else %} |
| 115 | +* [{{ site.framework }} Editor Product Page](https://www.telerik.com/aspnet-mvc/editor) |
| 116 | +
|
| 117 | +* [Telerik UI for {{ site.framework }} Video Onboarding Course (Free for trial users and license holders)]({%slug virtualclass_uiformvc%}) |
| 118 | +
|
| 119 | +* [Telerik UI for {{ site.framework }} Forums](https://www.telerik.com/forums/aspnet-mvc) |
| 120 | +{% endif %} |
| 121 | +
|
| 122 | +## See Also |
| 123 | +
|
| 124 | +* [Client-Side API Reference of the Editor for {{ site.framework }}](https://docs.telerik.com/kendo-ui/api/javascript/ui/editor) |
| 125 | +* [Server-Side API Reference of the Editor for {{ site.framework }}](https://docs.telerik.com/{{ site.platform }}/api/editor) |
| 126 | +{% if site.core %} |
| 127 | +* [Server-Side TagHelper API Reference of the Editor for {{ site.framework }}](https://docs.telerik.com/aspnet-core/api/taghelpers/editor) |
| 128 | +{% endif %} |
| 129 | +* [Telerik UI for {{ site.framework }} Breaking Changes]({%slug breakingchanges_2024%}) |
| 130 | +* [Telerik UI for {{ site.framework }} Knowledge Base](https://docs.telerik.com/{{ site.platform }}/knowledge-base) |
0 commit comments