Example of File Browser in ASP.NET MVC Rich Text Editor Control
This sample demonstrates the option to insert the image into the Rich Text Editor content using FileManager. Click the open folder button from the toolbar item to insert the image.
Choose a picture
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "browserEditor",
Name = "Browser",
Height = "360px",
ToolbarSettings = new UiRichTextToolbar
{
Items = (List<string>)ViewData["Tools"]
},
CustomTools = new List<UiRichTextCustomTool>
{
new UiRichTextCustomTool { Name = "FileBrowser", Text = "Browse", TooltipText = "Insert a picture from the server's files", OnClick = "RteFileBrowser.open" }
},
Value = (string)ViewData["RTEValue"]
})
@Html.UiFileManager(new UiFileManagerOptions
{
Id = "fileBrowser",
Url = Url.Action("GalleryFiles", "FileManagerDemo"),
ImageUrl = Url.Action("Image", "FileManagerDemo"),
View = UiFileManagerView.LargeIcons,
ReadOnly = true,
Height = "380px",
OnFileOpen = "RteFileBrowser.choose"
})
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "browserEditor",
Name = "Browser",
Height = "360px",
ToolbarSettings = new UiRichTextToolbar
{
Items = (List<string>)ViewData["Tools"]
},
CustomTools = new List<UiRichTextCustomTool>
{
new UiRichTextCustomTool { Name = "FileBrowser", Text = "Browse", TooltipText = "Insert a picture from the server's files", OnClick = "RteFileBrowser.open" }
},
Value = (string)ViewData["RTEValue"]
})
@using (Html.BeginUiDialog(new UiDialogOptions { Id = "fileBrowserDialog", Header = "Choose a picture", Width = "720px", ShowCloseIcon = true, IsModal = true }))
{
@Html.UiFileManager(new UiFileManagerOptions
{
Id = "fileBrowser",
Url = Url.Action("GalleryFiles", "FileManagerDemo"),
ImageUrl = Url.Action("Image", "FileManagerDemo"),
View = UiFileManagerView.LargeIcons,
ReadOnly = true,
Height = "380px",
OnFileOpen = "RteFileBrowser.choose"
})
}
<script>
window.RteFileBrowser = (function () {
'use strict';
var PICTURE = /\.(png|jpe?g|gif|webp)$/i;
/** The Browse tool's OnClick: the File Manager in its dialog (the editor keeps where the caret was). */
function open() {
window.Lumen.get('fileBrowserDialog').show();
}
/** The File Manager's OnFileOpen: a picture goes into the text; a folder opens as usual; another file is not downloaded. */
function choose(item) {
if (item.isFolder) { return true; }
if (!PICTURE.test(item.name)) { return false; }
var img = document.createElement('img');
img.setAttribute('src', '/FileManagerDemo/Image?path=' + encodeURIComponent(item.path));
img.setAttribute('alt', item.name.replace(/\.[^.]+$/, ''));
img.setAttribute('width', '320');
window.Lumen.get('fileBrowserDialog').hide();
window.Lumen.get('browserEditor').executeCommand('insertHTML', img.outerHTML);
return false;
}
return { open: open, choose: choose };
})();
</script>
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.IO;
using System.Web;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class RichTextEditorController : Controller
{
// The page is Views/RichTextEditor/FileBrowser.cshtml; it draws what ViewData holds.
public ActionResult FileBrowser()
{
ViewData["RTEValue"] = "<p>Put the caret here, then use <b>Browse</b>.</p>";
ViewData["Tools"] = new List<string> { "Bold", "Italic", "|", "Formats", "Alignments", "|", "FileBrowser", "Image", "|", "Undo", "Redo" };
return View();
}
/// <summary>The Image sample's uploads: kept in memory (the 50 newest), as a demo needs no storage of its own.</summary>
private static readonly ConcurrentDictionary<string, KeyValuePair<DateTime, byte[]>> Images = new ConcurrentDictionary<string, KeyValuePair<DateTime, byte[]>>();
/// <summary>An uploaded image, by the id SaveImage gave it.</summary>
public ActionResult Image(string id)
{
KeyValuePair<DateTime, byte[]> image;
if (id == null || !Images.TryGetValue(id, out image)) { return HttpNotFound(); }
Response.Headers["X-Content-Type-Options"] = "nosniff";
return File(image.Value, ImageType(image.Value));
}
private static string ImageType(byte[] b)
{
if (b.Length > 8 && b[0] == 0x89 && b[1] == 0x50 && b[2] == 0x4E && b[3] == 0x47) { return "image/png"; }
if (b.Length > 3 && b[0] == 0xFF && b[1] == 0xD8 && b[2] == 0xFF) { return "image/jpeg"; }
if (b.Length > 6 && b[0] == 0x47 && b[1] == 0x49 && b[2] == 0x46 && b[3] == 0x38) { return "image/gif"; }
if (b.Length > 12 && b[0] == 0x52 && b[1] == 0x49 && b[2] == 0x46 && b[3] == 0x46 && b[8] == 0x57 && b[9] == 0x45 && b[10] == 0x42 && b[11] == 0x50) { return "image/webp"; }
return null;
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
@* Rich Text Editors with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
The FileManager feature provides an option to insert the image into the editor and it supports various cloud services. It supports all the basic file operations such as create, rename, delete, cut, copy, paste, upload, download and so on.
Note: File Manager’s upload functionality is restricted in online demo.