Example of Custom Thumbnails in ASP.NET MVC File Manager Control
In this sample of the File Manager component, the custom thumbnail feature is showcased, allowing users to view personalized thumbnails for both folders and file types using the ShowThumbnail property.
@Html.AntiForgeryToken()
<h3>Custom thumbnails</h3>
<p>In the large icons view, pictures show themselves: the control asks the Image action for each one (PNG, JPEG, GIF, BMP and WebP only). OnFileLoad gets every item as it is drawn; here it gives PDF files an icon of the page's own.</p>
<style>
.navigation-sample .fm-custom-pdf .ui-fm-icon { --ui-fm-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M20 2H8c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zm-8.5 7.5c0 .83-.67 1.5-1.5 1.5H9v2H7.5V7H10c.83 0 1.5.67 1.5 1.5v1zm5 2c0 .83-.67 1.5-1.5 1.5h-2.5V7H15c.83 0 1.5.67 1.5 1.5v3zm4-3H19v1h1.5V11H19v2h-1.5V7h3v1.5zM9 9.5h1v-1H9v1zM4 6H2v14c0 1.1.9 2 2 2h14v-2H4V6zm10 5.5h1v-3h-1v3z'/%3E%3C/svg%3E"); color: #c62828; }
</style>
@Html.UiFileManager(new UiFileManagerOptions
{
Id = "fmThumbs",
Url = Url.Action("GalleryFiles", "FileManagerDemo"),
ImageUrl = Url.Action("Image", "FileManagerDemo"),
View = UiFileManagerView.LargeIcons,
ReadOnly = true,
Height = "420px",
OnFileLoad = "NavigationFileManager.onFileLoad",
OnFileOpen = "NavigationFileManager.cancelOpen",
OnSuccess = "NavigationFileManager.onSuccess",
OnFailure = "NavigationFileManager.onFailure"
})
@section scripts {
<script>
(function (window, document) {
'use strict';
function log(message) {
if (window.NavigationSampleLog) { window.NavigationSampleLog(message); }
}
// Never forward server errors, filenames, paths or file contents to the shared log.
window.NavigationFileManager = {
onSelect: function (items) { log('Selection count: ' + items.length); },
onSuccess: function () { log('Operation completed.'); },
onFailure: function () { log('Operation failed; details omitted.'); },
cancelOpen: function () { log('File opening cancelled.'); return false; },
// Custom thumbnails: PDF files get an icon of the page's own (the class is styled by the sample).
onFileLoad: function (args) {
if (args.module !== 'NavigationPane' && args.item.type === '.pdf') { args.element.classList.add('fm-custom-pdf'); }
}
};
document.querySelectorAll('.navigation-sample [data-filemanager-api]').forEach(function (button) {
button.addEventListener('click', function () {
var control = window.Lumen.get('navigationFileManager');
if (!control) { log('Control not ready.'); return; }
var operation = this.getAttribute('data-filemanager-api');
var pending;
if (operation === 'folder') { pending = control.openFolder('/Documents/Reports'); }
if (operation === 'refresh') { pending = control.refresh(); }
if (operation === 'inspect') {
log('Folder available: ' + Boolean(control.getPath()) + '; selection count: ' + control.getSelected().length);
}
if (operation === 'view') {
control.setView(control.getView() === 'details' ? 'largeicons' : 'details');
log('View changed.');
}
if (pending && pending.catch) { pending.catch(function () { log('API request failed; details omitted.'); }); }
});
});
document.querySelectorAll('.navigation-sample [data-filemanager-flat]').forEach(function (button) {
button.addEventListener('click', function () {
var control = window.Lumen.get('fmFlat');
var data = control && control.getFileSystemData();
log(data ? 'The list holds ' + data.length + ' items.' : 'Control not ready.');
});
});
document.querySelectorAll('.navigation-sample [data-filemanager-virtual]').forEach(function (button) {
button.addEventListener('click', function () {
var control = window.Lumen.get('fmVirtual');
if (control) { control.setView(control.getView() === 'details' ? 'largeicons' : 'details'); log('View changed.'); }
});
});
var picker = document.getElementById('fileManagerTheme');
if (picker) {
picker.addEventListener('change', function () {
var control = window.Lumen.get('navigationFileManager');
if (control) { control.setTheme(this.value); log('Instance theme changed.'); }
});
}
})(window, document);
</script>
}
<div class="navigation-sample">
<style>
.navigation-sample .nav-row { display: flex; flex-wrap: wrap; gap: 16px; align-items: flex-start; }
.navigation-sample .nav-row > * { flex: 1 1 280px; min-width: 0; }
.navigation-sample .nav-caption { margin: 16px 0 6px; font-weight: 600; }
.navigation-sample .nav-frame { height: 260px; border: 1px solid var(--ui-border); border-radius: 4px; }
.navigation-sample .nav-main { height: 100%; box-sizing: border-box; padding: 12px 16px; overflow: auto; }
.navigation-sample .nav-side { padding: 12px; }
.navigation-sample .nav-files { display: flex; flex-wrap: wrap; gap: 12px; }
.navigation-sample .nav-file { width: 150px; padding: 12px; border: 1px solid var(--ui-border); cursor: context-menu; }
.navigation-sample .nav-file:focus-visible { outline: 2px solid var(--ui-accent); }
.navigation-sample .nav-file small { display: block; }
.navigation-sample .ui-tab-panel { padding: 12px 4px; }
.navigation-sample .nav-slide { height: 100%; display: flex; align-items: center; justify-content: center; color: white; font-size: 28px; }
.navigation-sample .nav-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 16px 0; }
.navigation-sample .nav-log { font-family: monospace; font-size: 12px; max-height: 200px; overflow-y: auto; border: 1px solid var(--ui-border); padding: 8px 24px; }
.navigation-sample .nav-icon { width: 16px; height: 16px; background: currentColor; clip-path: polygon(50% 0, 100% 40%, 85% 40%, 85% 100%, 15% 100%, 15% 40%, 0 40%); }
</style>
<p role="note">Use synthetic demo files only. Creating, renaming, moving, copying, uploading or deleting changes files on disk in the demo's restricted App_Data folder. Never upload personal, client or confidential files. Uploads are limited to 2 MB. Read-only previews enforce changes being refused on the server.</p>
<p>These demo actions are not a production authorisation model: the responsible security reviewer must validate host authentication, authorisation and filesystem permissions before adoption.</p>
@Html.AntiForgeryToken()
<h3>Custom thumbnails</h3>
<p>In the large icons view, pictures show themselves: the control asks the Image action for each one (PNG, JPEG, GIF, BMP and WebP only). OnFileLoad gets every item as it is drawn; here it gives PDF files an icon of the page's own.</p>
<style>
.navigation-sample .fm-custom-pdf .ui-fm-icon { --ui-fm-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M20 2H8c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zm-8.5 7.5c0 .83-.67 1.5-1.5 1.5H9v2H7.5V7H10c.83 0 1.5.67 1.5 1.5v1zm5 2c0 .83-.67 1.5-1.5 1.5h-2.5V7H15c.83 0 1.5.67 1.5 1.5v3zm4-3H19v1h1.5V11H19v2h-1.5V7h3v1.5zM9 9.5h1v-1H9v1zM4 6H2v14c0 1.1.9 2 2 2h14v-2H4V6zm10 5.5h1v-3h-1v3z'/%3E%3C/svg%3E"); color: #c62828; }
</style>
@Html.UiFileManager(new UiFileManagerOptions
{
Id = "fmThumbs",
Url = Url.Action("GalleryFiles", "FileManagerDemo"),
ImageUrl = Url.Action("Image", "FileManagerDemo"),
View = UiFileManagerView.LargeIcons,
ReadOnly = true,
Height = "420px",
OnFileLoad = "NavigationFileManager.onFileLoad",
OnFileOpen = "NavigationFileManager.cancelOpen",
OnSuccess = "NavigationFileManager.onSuccess",
OnFailure = "NavigationFileManager.onFailure"
})
</div>
@section below {
<div class="navigation-sample">
<h3>Event log</h3>
<ul id="navigationEventLog" class="nav-log" aria-live="polite"></ul>
<script>
window.NavigationSampleLog = function (message) {
'use strict';
var list = document.getElementById('navigationEventLog');
if (!list) { return; }
var item = document.createElement('li');
item.textContent = new Date().toLocaleTimeString() + ' - ' + message;
list.insertBefore(item, list.firstChild);
while (list.children.length > 100) { list.removeChild(list.lastChild); }
};
</script>
</div>
}
@section scripts {
<script>
(function (window, document) {
'use strict';
function log(message) {
if (window.NavigationSampleLog) { window.NavigationSampleLog(message); }
}
// Never forward server errors, filenames, paths or file contents to the shared log.
window.NavigationFileManager = {
onSelect: function (items) { log('Selection count: ' + items.length); },
onSuccess: function () { log('Operation completed.'); },
onFailure: function () { log('Operation failed; details omitted.'); },
cancelOpen: function () { log('File opening cancelled.'); return false; },
// Custom thumbnails: PDF files get an icon of the page's own (the class is styled by the sample).
onFileLoad: function (args) {
if (args.module !== 'NavigationPane' && args.item.type === '.pdf') { args.element.classList.add('fm-custom-pdf'); }
}
};
document.querySelectorAll('.navigation-sample [data-filemanager-api]').forEach(function (button) {
button.addEventListener('click', function () {
var control = window.Lumen.get('navigationFileManager');
if (!control) { log('Control not ready.'); return; }
var operation = this.getAttribute('data-filemanager-api');
var pending;
if (operation === 'folder') { pending = control.openFolder('/Documents/Reports'); }
if (operation === 'refresh') { pending = control.refresh(); }
if (operation === 'inspect') {
log('Folder available: ' + Boolean(control.getPath()) + '; selection count: ' + control.getSelected().length);
}
if (operation === 'view') {
control.setView(control.getView() === 'details' ? 'largeicons' : 'details');
log('View changed.');
}
if (pending && pending.catch) { pending.catch(function () { log('API request failed; details omitted.'); }); }
});
});
document.querySelectorAll('.navigation-sample [data-filemanager-flat]').forEach(function (button) {
button.addEventListener('click', function () {
var control = window.Lumen.get('fmFlat');
var data = control && control.getFileSystemData();
log(data ? 'The list holds ' + data.length + ' items.' : 'Control not ready.');
});
});
document.querySelectorAll('.navigation-sample [data-filemanager-virtual]').forEach(function (button) {
button.addEventListener('click', function () {
var control = window.Lumen.get('fmVirtual');
if (control) { control.setView(control.getView() === 'details' ? 'largeicons' : 'details'); log('View changed.'); }
});
});
var picker = document.getElementById('fileManagerTheme');
if (picker) {
picker.addEventListener('change', function () {
var control = window.Lumen.get('navigationFileManager');
if (control) { control.setTheme(this.value); log('Instance theme changed.'); }
});
}
})(window, document);
</script>
}
using System;
using System.Configuration;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
using IOFile = System.IO.File; // Controller has a File() method of its own
namespace Lumen.UI.Mvc.Demo.Controllers
{
/// <summary>
/// The file manager's three actions over one folder, ~/App_Data/FileManagerDemo. App_Data is not
/// served by IIS, so nothing uploaded here can be opened (or run) by its address.
/// A real app puts its own [Authorize] rules on these actions: they decide who may see and change the files.
/// </summary>
public class FileManagerDemoController : Controller
{
/// <summary>The picture of an image of the gallery, for the thumbnails (ImageUrl). A GET, as an img asks for it.</summary>
[HttpGet]
public ActionResult Image(string path)
{
return Gallery().Image(path);
}
/// <summary>The gallery folder, read-only: thumbnails and templates.</summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult GalleryFiles(UiFileManagerRequest request)
{
return Json(Gallery().Execute(request));
}
private UiPhysicalFileProvider Gallery()
{
string root = Server.MapPath("~/App_Data/FileManagerGallery");
if (!Directory.Exists(root)) { CreateGallery(root); }
return new UiPhysicalFileProvider(root) { RootName = "Gallery", ReadOnly = true };
}
/// <summary>Pictures drawn here (no photos of anyone), a PDF and some text.</summary>
private static void CreateGallery(string root)
{
Directory.CreateDirectory(Path.Combine(root, "Photos"));
Directory.CreateDirectory(Path.Combine(root, "Documents"));
Picture(Path.Combine(root, "Logo.png"), Color.FromArgb(37, 99, 235), Color.FromArgb(14, 165, 233), ImageFormat.Png);
Picture(Path.Combine(root, "Banner.png"), Color.FromArgb(234, 88, 12), Color.FromArgb(250, 204, 21), ImageFormat.Png);
Picture(Path.Combine(root, "Photos", "Lake.jpg"), Color.FromArgb(13, 148, 136), Color.FromArgb(30, 64, 175), ImageFormat.Jpeg);
Picture(Path.Combine(root, "Photos", "Hills.jpg"), Color.FromArgb(22, 163, 74), Color.FromArgb(202, 138, 4), ImageFormat.Jpeg);
IOFile.WriteAllText(Path.Combine(root, "Guide.pdf"), "%PDF-1.4" + Environment.NewLine + "% A stand-in PDF for the demo." + Environment.NewLine);
IOFile.WriteAllText(Path.Combine(root, "Read me.txt"), "Pictures and documents for the thumbnail and template samples." + Environment.NewLine);
IOFile.WriteAllText(Path.Combine(root, "Documents", "Plan.txt"), "A plan." + Environment.NewLine);
}
/// <summary>A 160 x 120 picture: a gradient with a circle.</summary>
private static void Picture(string file, Color from, Color to, ImageFormat format)
{
using (var bitmap = new Bitmap(160, 120))
using (Graphics g = Graphics.FromImage(bitmap))
using (var fill = new LinearGradientBrush(new Rectangle(0, 0, 160, 120), from, to, 45f))
using (var sun = new SolidBrush(Color.FromArgb(200, 255, 255, 255)))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.FillRectangle(fill, 0, 0, 160, 120);
g.FillEllipse(sun, 98, 18, 40, 40);
bitmap.Save(file, format);
}
}
}
}
@* 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>
@* Controls with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
In this demo, custom thumbnail styles (background-image for the following classes within the .e-filemanager .e-large-icons class) were applied to folder and file types using corresponding class names, resulting in a visually distinctive representation of the thumbnails according to their specific needs.
.e-fe-image.e-fe-music.e-fe-xlsx.e-fe-video.e-fe-pptx.e-fe-rar.e-fe-zip.e-fe-txt.e-fe-js.e-fe-css.e-fe-html.e-fe-unknown.e-fe-exe.e-fe-msi.e-fe-php.e-fe-doc.e-fe-docx.e-fe-xml.e-fe-folder
Note: File Manager's upload functionality is restricted in the online demos for security reasons. If you need to test upload functionality, please install Syncfusion Essential Studio® on your machine and run the demo.