Check file size before uploading it

21 Dec 20221 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.

@using Syncfusion.EJ2

<div class="col-lg-8 control-section">
    <div class="control_wrapper">
        @Html.EJS().Uploader("UploadFiles").Uploading("onBeforeUpload").AutoUpload(false).AsyncSettings(new Syncfusion.EJ2.Inputs.UploaderAsyncSettings { SaveUrl = "https://services.syncfusion.com/aspnet/production/api/FileUploader/Save", RemoveUrl = "https://services.syncfusion.com/aspnet/production/api/FileUploader/Remove" }).Render()
    </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>
.control_wrapper {
        max-width: 500px;
        margin: auto;
    }

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

NOTE

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