InputFile maximum file amount in blazor

Anna Bogdanovna Bondarets 20 Reputation points
2024-04-11T15:29:23.18+00:00

Hello, I implemented InputFile in blazor app to get multiple files in one go. However, when I upload more than 10 files I get the warning that you can see in the attached screenshot. I put private int maxAllowedFiles = 20; but it doesn't seem to affect the execution. Every time I get these warnings the app just freezes. I wanted to limit the upload of files by giving a warning, but it seems not to enter the method where I count the amount of uploaded files with int numberOfFilesUploaded = e.GetMultipleFiles().Count(); Could you please recommend a way to avoid this behavior? Is there a way to count the amount of files before it gets to the method?

User's image

Developer technologies .NET Blazor
{count} votes

Accepted answer
  1. Anonymous
    2024-04-12T06:49:57.6433333+00:00

    Hi @Anna Bogdanovna Bondarets,

    I think you need check the source code of GetMultipleFiles:

    public IReadOnlyList<IBrowserFile> GetMultipleFiles(int maximumFileCount = 10)
    {
        if (_files.Count > maximumFileCount)
        {
            throw new InvalidOperationException($"The maximum number of files accepted is {maximumFileCount}, but {_files.Count} were supplied.");
        }
        return _files;
    }
    

    To achieve your requirement, you need change your code like below:

    int numberOfFilesUploaded = e.GetMultipleFiles(maxAllowedFiles).Count();
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Best regards,
    Rena

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2024-04-11T17:11:22.4233333+00:00

    The Blazor documentation has an example of limiting the number of upload files.

    https://learn.microsoft.com/en-us/aspnet/core/blazor/file-uploads?view=aspnetcore-8.0#file-size-read-and-upload-limits


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.