Blazor FileInput
The Blazor Bootstrap FileInput component provides a standard Bootstrap file control with validation and removal.
Basic usage
# FileInput uses Bootstrap's standard visible file-control presentation. Use DragAndDropFileInput when a drop area is needed.
Example View Source
<FileInput HintText="Select a file with the standard Bootstrap file control." />
Multiple files and removal
# Set Multiple to allow a selected file to be removed without clearing the others.
Example View Source
<FileInput Multiple="true" HintText="Select multiple files, then remove individual files from the list." />
Sizes
# Use Size to apply Bootstrap's form-control-lg and form-control-sm file-control sizes.
Example View Source
<div class="vstack gap-3">
<FileInput Size="FileInputSize.Large" HintText="Large file input." />
<FileInput Size="FileInputSize.Small" HintText="Small file input." />
</div>
Validation
# FileInput validates sizes, counts, and extensions selected from the file dialog. Configure validation-message templates with {FileName}, {MaxFileSize}, {AllowedFileTypes}, or {MaxFileCount} where applicable; the single-file message is static.
Example View Source
<FileInput Multiple="true"
MaxFileCount="3"
MaxFileSize="1048576"
AllowedFileTypes="@allowedFileTypes"
InvalidFileTypeErrorMessage="{FileName} must use one of: {AllowedFileTypes}."
MaxFileCountErrorMessage="{FileName} would exceed the {MaxFileCount}-file limit."
MaxFileSizeErrorMessage="{FileName} exceeds the {MaxFileSize}-byte limit."
SingleFileErrorMessage="Select only one file."
HintText="Select up to three PDF or PNG files, each no larger than 1 MB." />
@code {
private readonly string[] allowedFileTypes = [".pdf", "png"];
}
Use FilesChanged to update a form model and combine file-selection rules with standard form validation.
Example View Source
@using System.ComponentModel.DataAnnotations
@using Microsoft.AspNetCore.Components.Forms
<EditForm EditContext="@editContext" OnValidSubmit="HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="mb-3">
<label class="form-label">Supporting document <span class="text-danger">*</span></label>
<FileInput AllowedFileTypes="@allowedFileTypes"
MaxFileSize="1048576"
FilesChanged="OnFilesChanged"
HintText="A PDF document up to 1 MB is required." />
<ValidationMessage For="@(() => upload.DocumentName)" />
</div>
<Button Type="ButtonType.Submit" Color="ButtonColor.Primary">Submit</Button>
@if (submitted)
{
<div class="alert alert-success mt-3" role="status">Form submitted with @upload.DocumentName.</div>
}
</EditForm>
@code {
private readonly string[] allowedFileTypes = ["pdf"];
private readonly UploadModel upload = new();
private EditContext editContext = null!;
private bool submitted;
protected override void OnInitialized()
{
editContext = new EditContext(upload);
}
private void HandleValidSubmit() => submitted = true;
private void OnFilesChanged(IReadOnlyList<IBrowserFile> files)
{
upload.DocumentName = files.FirstOrDefault()?.Name;
submitted = false;
editContext.NotifyFieldChanged(new FieldIdentifier(upload, nameof(upload.DocumentName)));
}
private sealed class UploadModel
{
[Required(ErrorMessage = "A supporting document is required.")]
public string? DocumentName { get; set; }
}
}
Hint text and unrestricted extensions
# An empty AllowedFileTypes collection accepts every extension while other configured rules remain active.
Example View Source
<FileInput Multiple="true"
AllowedFileTypes="@allowedFileTypes"
MaxFileSize="5242880"
HintText="Any extension is accepted because the allowed-extension collection is empty; the 5 MB size limit still applies." />
@code {
private readonly string[] allowedFileTypes = [];
}