Example of Form Support in ASP.NET MVC FileUpload Control
This ASP.NET MVC File Upload example demonstrates the Uploader control supported with HTML form upload. Fill the mandatory details in a form and click the submit button.
Photo Contest
@Html.UiTextBox("name", null, new UiTextBoxOptions { Label = "Name", FloatLabelType = UiFloatLabelType.Always })
@Html.UiTextBox("email", null, new UiTextBoxOptions { Label = "Email", FloatLabelType = UiFloatLabelType.Always, Type = UiTextBoxType.Email })
@Html.UiTextBox("mobile", null, new UiTextBoxOptions { Label = "Mobile No", FloatLabelType = UiFloatLabelType.Always })
@Html.UiFileUpload("fileupload", new UiFileUploadOptions { Label = "Choose a file", Multiple = false, AutoUpload = false, AllowedExtensions = ".png,.jpg,.jpeg,.gif,.webp", OnChange = "onFileSelect" })
@Html.UiTextArea("Address", null, new UiTextAreaOptions { Label = "Address", FloatLabelType = UiFloatLabelType.Always, Rows = 4 })
@Html.UiButton(new UiButtonOptions { Id = "submit-btn", Text = "Submit", Color = UiButtonColor.Primary })
@Html.UiDialog(new UiDialogOptions { Id = "confirmationDialog", Header = "Success", Content = "Your details have been updated successfully, Thank you.", ShowCloseIcon = true, Width = "335px", Animation = UiDialogAnimation.Zoom })
<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; }
.form-title { text-align: center; margin: 12px 0; }
.form-group { padding-top: 11px; }
.form-group .error { color: #d13438; font-size: 12px; min-height: 14px; }
.submitBtn { margin-top: 16px; text-align: center; }
.submitBtn .desc { margin-top: 8px; font-size: 12px; color: var(--ui-muted); }
</style>
<div class="control-section">
<h4 class="form-title">Photo Contest</h4>
<div class="control_wrapper" id="control_wrapper">
<form id="form1" method="post" onsubmit="return false;">
<div class="form-group">@Html.UiTextBox("name", null, new UiTextBoxOptions { Label = "Name", FloatLabelType = UiFloatLabelType.Always })<div id="nameError" class="error"></div></div>
<div class="form-group">@Html.UiTextBox("email", null, new UiTextBoxOptions { Label = "Email", FloatLabelType = UiFloatLabelType.Always, Type = UiTextBoxType.Email })<div id="mailError" class="error"></div></div>
<div class="form-group">@Html.UiTextBox("mobile", null, new UiTextBoxOptions { Label = "Mobile No", FloatLabelType = UiFloatLabelType.Always })<div id="noError" class="error"></div></div>
<div class="form-group">
@Html.UiFileUpload("fileupload", new UiFileUploadOptions { Label = "Choose a file", Multiple = false, AutoUpload = false, AllowedExtensions = ".png,.jpg,.jpeg,.gif,.webp", OnChange = "onFileSelect" })
<div id="uploadError" class="error"></div>
</div>
<div class="form-group">@Html.UiTextArea("Address", null, new UiTextAreaOptions { Label = "Address", FloatLabelType = UiFloatLabelType.Always, Rows = 4 })</div>
</form>
<div class="submitBtn">
@Html.UiButton(new UiButtonOptions { Id = "submit-btn", Text = "Submit", Color = UiButtonColor.Primary })
<div class="desc"><span>*This button is not a submit type and the form submit handled from externally.</span></div>
</div>
@Html.UiDialog(new UiDialogOptions { Id = "confirmationDialog", Header = "Success", Content = "Your details have been updated successfully, Thank you.", ShowCloseIcon = true, Width = "335px", Animation = UiDialogAnimation.Zoom })
</div>
</div>
<script>
// Syncfusion: the form is checked from outside (name, email, mobile and a file are required); a good form is cleared and the dialog says thank you
var fileChosen = false;
function onFileSelect(files) { fileChosen = !!(files && (files.length === undefined ? files : files.length)); }
function setError(id, text) { document.getElementById(id).textContent = text; }
document.getElementById('submit-btn').addEventListener('click', function () {
var ok = true;
var upload = window.Lumen.get('fileupload');
var rules = [['name', 'nameError', '* Enter your name'], ['email', 'mailError', '* Please enter valid email'], ['mobile', 'noError', '* Enter your mobile number']];
rules.forEach(function (r) {
var empty = !document.getElementById(r[0]).value.trim();
setError(r[1], empty ? r[2] : '');
if (empty) { ok = false; }
});
var chosen = upload.getFiles().length > 0;
setError('uploadError', chosen ? '' : '* Select any file');
if (!chosen) { ok = false; }
if (!ok) { return; }
document.getElementById('form1').reset();
upload.clear();
window.Lumen.get('confirmationDialog').show();
});
</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/FormSupport.cshtml; it draws what ViewData holds.
public ActionResult FormSupport()
{
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>
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.
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.