Example of Chunk Upload in ASP.NET MVC FileUpload Control
This ASP.NET MVC File Upload example demonstrates the chunk upload functionalities of the Uploader component. Browse or drag-and-drop a large file to upload with pause, resume, and retry options. Also, configured property panel to change the chunk size dynamically.
@Html.UiFileUpload("UploadFiles", new UiFileUploadOptions { Label = "Large dummy file", Multiple = false, AutoUpload = false, SaveUrl = Url.Action("SaveChunk", "UploadDemo"), RemoveUrl = Url.Action("Remove", "UploadDemo"), ChunkSize = 500000, RetryCount = 3, SequentialUpload = true, MaxFileSize = 104857600 })
@Html.UiDropDownList("chunksize", new UiDropDownListOptions { Width = "120px", Value = "500000", OnChange = "valueChange", Items = ((IEnumerable<InputsDemoController.chunkValues>)ViewData["data"]).Select(c => new UiListItem(c.chunkValue, c.text)).ToList() })
<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>
@using Lumen.UI.Mvc.Demo.Controllers
<div class="control-section">
<div class="control_wrapper">
@Html.UiFileUpload("UploadFiles", new UiFileUploadOptions { Label = "Large dummy file", Multiple = false, AutoUpload = false, SaveUrl = Url.Action("SaveChunk", "UploadDemo"), RemoveUrl = Url.Action("Remove", "UploadDemo"), ChunkSize = 500000, RetryCount = 3, SequentialUpload = true, MaxFileSize = 104857600 })
</div>
</div>
<div class="demo-property-panel property-section" title="Properties">
<div class="property-row"><span>Chunk Size:</span> @Html.UiDropDownList("chunksize", new UiDropDownListOptions { Width = "120px", Value = "500000", OnChange = "valueChange", Items = ((IEnumerable<InputsDemoController.chunkValues>)ViewData["data"]).Select(c => new UiListItem(c.chunkValue, c.text)).ToList() })</div>
</div>
<script>
// Syncfusion: the list sets the size of the pieces a file is sent in
function valueChange(value) { if (value) { window.Lumen.get('UploadFiles').setChunkSize(parseInt(value, 10)); } }
</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/ChunkUpload.cshtml; it draws what ViewData holds.
public ActionResult ChunkUpload()
{
ViewData["data"] = new chunkValues().UploaderModel();
return View();
}
public class chunkValues
{
public string text { get; set; }
public string chunkValue { get; set; }
public List<chunkValues> UploaderModel()
{
List<chunkValues> list = new List<chunkValues>();
list.Add(new chunkValues { text = "500 KB", chunkValue = "500000" });
list.Add(new chunkValues { text = "1 MB", chunkValue = "1000000" });
list.Add(new chunkValues { text = "2 MB", chunkValue = "2000000" });
list.Add(new chunkValues { text = "5 MB", chunkValue = "5000000" });
return list;
}
}
}
}
@* 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>
When the file size is large or transfer the file with slow network connection, the chunk upload feature slices the files and upload the sliced chunks to server in sequential order using the chunkSize API.
The sample is configured with the following options:
- While uploading, you can pause the upload and resume it later.
- If the upload fails, retry option will be enabled.
- The sample is configured with maximum file size as `100 MB` to upload.
If the application lost its connection (offline), the upload component pauses the process automatically. After the connection is up (online), the upload component will resume its process.
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.