Search results

Check File Size before Uploading it in ASP.NET Core Uploader control

02 Aug 2021 / 1 minute to read

By using uploading event, you can get the file size before upload it to server. File object contains the file size in bytes only. You can convert the size to standard formats (KB or MB) using bytesToSize method.

tagHelper
check-file-size.cs
index.css
Copied to clipboard
@{
    var asyncSettings = new Syncfusion.EJ2.Inputs.UploaderAsyncSettings { SaveUrl = "https://ej2.syncfusion.com/services/api/uploadbox/Save", RemoveUrl = "https://ej2.syncfusion.com/services/api/uploadbox/Remove" };
}

<div class="col-lg-8 control-section">
    <div class="control_wrapper">
        <ejs-uploader id="UploadFiles" asyncSettings="@asyncSettings" autoUpload="false" uploading="onBeforeUpload">
        </ejs-uploader>
    </div>
</div>

<script>
    function onBeforeUpload(args) {
        var uploadObj = document.getElementById("UploadFiles").ej2_instances[0];
        // get the file size in bytes
        var sizeInBytes = args.fileData.size;
        // get the file size in standard format
        alert("File size is: " + uploadObj.bytesToSize(sizeInBytes))
    }
</script>
Copied to clipboard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EJ2CoreSampleBrowser.Controllers.TextBoxes
{
    public partial class UploaderController : Controller
    {
        public ActionResult DefaultFunctionalities()
        {
            
            return View();
        }
    }
}
Copied to clipboard
.control_wrapper {
        max-width: 500px;
        margin: auto;
    }

    .control_wrapper .e-upload .e-upload-drag-hover {
        margin: 0;
    }

You can also explore ASP.NET Core File Upload feature tour page for its groundbreaking features. You can also explore our ASP.NET Core File Upload example to understand how to browse the files which you want to upload to the server.