Example of Image Preview in ASP.NET MVC FileUpload Control
This ASP.NET MVC File Upload example demonstrates how to add an image preview of the uploaded files. Browse or drag-and-drop image files (PNG, JPG) to display preview for the selected files.
@Html.UiFileUpload("previewFiles", new UiFileUploadOptions { Id = "uploadPreview", Label = "Drop image (JPG, PNG) files here or browse", AllowedExtensions = ".jpg,.png,.jpeg", AutoUpload = false, ShowImagePreview = true })
@Html.UiButton(new UiButtonOptions { Id = "clearbtn", Text = "Clear All" })
@Html.UiButton(new UiButtonOptions { Id = "uploadbtn", Text = "Upload All" })
<style>
.control_wrapper { max-width: 420px; margin: 0 auto; padding: 20px 0; }
.input-drop { padding: 24px; border: 2px dashed var(--ui-border); text-align: center; }
.property-section .property-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; }
</style>
<div class="control-section"><div class="control_wrapper">
@Html.UiFileUpload("previewFiles", new UiFileUploadOptions { Id = "uploadPreview", Label = "Drop image (JPG, PNG) files here or browse", AllowedExtensions = ".jpg,.png,.jpeg", AutoUpload = false, ShowImagePreview = true })
</div></div>
<div class="demo-property-panel property-section" title="Properties">
<div class="property-row">@Html.UiButton(new UiButtonOptions { Id = "clearbtn", Text = "Clear All" })</div>
<div class="property-row">@Html.UiButton(new UiButtonOptions { Id = "uploadbtn", Text = "Upload All" })</div>
</div>
<script>
// Syncfusion: Clear All empties the list, Upload All sends the images that wait
document.getElementById('clearbtn').addEventListener('click', function () { window.Lumen.get('uploadPreview').clear(); });
document.getElementById('uploadbtn').addEventListener('click', function () { window.Lumen.get('uploadPreview').upload(); });
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class UploaderController : Controller
{
// The page is Views/Uploader/ImagePreview.cshtml; it draws what ViewData holds.
public ActionResult ImagePreview()
{
return View();
}
}
}
@* 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>
The Uploader control allows to create preview images after uploaded it. The preview images created by reading the file using success event. Also, the user can create preview images before uploading to server using select event.
In this example, the backend service used in the saveUrl and removeUrl endpoints for saving and removing files is intended for demonstration purposes only. The uploaded files are subjected to thorough validation, including verification of file names and the application of security checks. Therefore, this service is not recommended for production use, and the configuration of a custom backend save and remove service is advised. Additional implementation details can be found in the saveUrl and removeUrl documentation.