Example of Preload Files in ASP.NET MVC FileUpload Control
This ASP.NET MVC File Upload example demonstrates how to pre-load the files of the Uploader. The already uploaded files are configured in file list to view and remove them.
@Html.UiFileUpload("preloadFiles", new UiFileUploadOptions { Id = "uploadPreload", Label = "Attachments", SaveUrl = Url.Action("Save", "UploadDemo"), RemoveUrl = Url.Action("Remove", "UploadDemo"), Files = uploaded, MaxFileSize = 1048576 })
@Html.UiButton(new UiButtonOptions { Id = "clearbtn", Text = "Clear 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>
@{
var uploaded = new[]
{
new UiUploadedFile { Id = "demo-contract", Name = "contract.pdf", Size = 182340 },
new UiUploadedFile { Id = "demo-photo", Name = "photo.jpg", Size = 524288 }
};
}
<div class="control-section"><div class="control_wrapper">
@Html.UiFileUpload("preloadFiles", new UiFileUploadOptions { Id = "uploadPreload", Label = "Attachments", SaveUrl = Url.Action("Save", "UploadDemo"), RemoveUrl = Url.Action("Remove", "UploadDemo"), Files = uploaded, MaxFileSize = 1048576 })
</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>
<script>
// Syncfusion: Clear All empties the list of the file upload
document.getElementById('clearbtn').addEventListener('click', function () { window.Lumen.get('uploadPreload').clear(); });
</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/PreloadFiles.cshtml; it draws what ViewData holds.
public ActionResult PreloadFiles()
{
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 load initial list of files which are already uploaded in server. The preload files are useful to view and remove from server. Also, you can achieve state persistence on page refresh.
To achieve state persistence, you can refer to this How-to section.
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.