How to limit the number of files that can be uploaded ?

Hello, 

Is there an in-built way to limit the number of files that can be uploaded (Exemple: maximum 10 files) via SfUploader ? 

Thanks in advance for the answer.


3 Replies

UD UdhayaKumar Duraisamy Syncfusion Team October 29, 2024 04:42 PM UTC

To limit the number of files that can be uploaded in the Syncfusion Blazor File Upload component, you can use the FileSelected event.

Here's an example of how to implement this:
<div style="margin:100px auto; width:300px">
    <SfUploader AllowMultiple="true" AutoUpload="false">
        <UploaderEvents FileSelected="@FileSelectedHandler"></UploaderEvents>
    </SfUploader>
    @if(IsBool){
        <span class="e-error">Maximum 5 files are allowed</span>
    }
</div>
 
@code {
    bool IsBool = false;
    private void FileSelectedHandler(SelectedEventArgs args)
    {
        if(args.FilesData.Count > 5 )
        {
            args.Cancel = true;
            IsBool = true;
        }
        else
        {
            IsBool = false;
        }
    }
}

  • FileSelected Event: This event checks the number of selected files and cancels the selection if it exceeds 5.
  • IsBool: A boolean flag used to display an error message if the file limit is exceeded.


ES Eléonore Stultjens October 30, 2024 10:15 AM UTC

Hello, 

Thank you for the solution ! 



SS Sarasilmiya Shahul Hameed Syncfusion Team November 1, 2024 01:38 PM UTC

You're welcome, please get back to us if you need any other assistance. 


Loader.
Up arrow icon