FileUpload
A drop zone around a native file input, with a file list, remove buttons and size, count and type limits.
On this page
Examples
In a form
Static rendering: the form posts the files (multipart) and they bind to a property of type IReadOnlyList<IBrowserFile> named like the input. Interactive rendering: OnChange receives them from InputFile.
Razor
<form method="post" @formname="documents" @onsubmit="Save" enctype="multipart/form-data" class="flex w-full max-w-md flex-col items-start gap-3">
<AntiforgeryToken />
<KlyFormField Label="Documents" class="w-full">
<KlyFileUpload Name="Files" Multiple Accept=".txt,.pdf,image/*" MaxSize="1048576" MaxFiles="3" Hint="Text, PDF or images" OnChange="OnChange" data-testid="documents" />
</KlyFormField>
@if (_chosen.Count > 0)
{
@* Interactive rendering: shows that .NET has the files, so Upload sends them. *@
<p class="text-sm text-(--klyroo-text-muted)" data-testid="documents-chosen">Ready to send: @_chosen.Count file(s)</p>
}
<KlyButton Type="ButtonType.Submit" Label="Upload" />
@if (_saved)
{
<p role="status" class="text-sm text-(--klyroo-success-text)" data-testid="documents-saved">Received: @(_received.Count == 0 ? "nothing" : string.Join(", ", _received))</p>
}
</form>
@code {
// Static rendering: the browser posts the files and Blazor binds them to this property by the name of the input.
[SupplyParameterFromForm(FormName = "documents")]
private IReadOnlyList<IBrowserFile>? Files { get; set; }
// Interactive rendering: InputFile raises OnChange as soon as files are chosen.
private IReadOnlyList<IBrowserFile> _chosen = [];
private IReadOnlyList<string> _received = [];
private bool _saved;
private void OnChange(InputFileChangeEventArgs e) => _chosen = e.GetMultipleFiles(3);
private void Save()
{
_received = (Files ?? _chosen).Select(f => f.Name).ToList();
_saved = true;
}
}Interactive
OnChange renders Blazor's InputFile, so reading the file needs an interactive render mode.
No file read yet.
Razor
<div class="flex w-full max-w-md flex-col gap-2">
<KlyFileUpload Accept="image/*" Label="Choose an avatar" Hint="PNG or JPG" MaxSize="2097152" OnChange="OnChange" data-testid="avatar" />
<p class="text-sm text-(--klyroo-text-muted)" data-testid="avatar-state">@_state</p>
</div>
@code {
private string _state = "No file read yet.";
private async Task OnChange(InputFileChangeEventArgs e)
{
await using var stream = e.File.OpenReadStream(2 * 1024 * 1024);
var buffer = new byte[stream.Length];
await stream.ReadExactlyAsync(buffer);
_state = $"Read {buffer.Length} bytes of {e.File.Name}.";
}
}API
KlyFileUpload
File input with a drop zone, a list of the chosen files with remove buttons, and limits on size, count and type.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
Accept |
string |
The native accept attribute: comma separated extensions ( .png ) and MIME types ( image/* ). Also enforced on dropped files. | |
Class |
string |
Classes added to the root element, merged last so they win over the theme. | |
Color |
KlyColor |
"primary" |
Semantic color of the drop zone: a built-in KlyColor or the name of a color you defined. Values: primary, secondary, success, info, warning, error, neutral. |
Disabled |
bool |
false |
Disables the input and the drop zone. |
Hint |
string |
Secondary text of the drop zone, such as the accepted formats. | |
Id |
string |
The id of the file input, which a label can point at. | |
Label |
string |
Main text of the drop zone. Defaults to DropFiles . | |
MaxFiles |
int? |
Most files kept at once. Defaults to 1 unless Multiple . | |
MaxSize |
Int64? |
Largest file, in bytes. Larger files are refused in the browser with a message, before anything is sent. | |
Multiple |
bool |
false |
Allows choosing several files. |
Name |
string |
Form field name. A static form posts the chosen files under it. | |
Required |
bool |
false |
Marks the input as required for the browser and for assistive technology. |
Size |
KlySize? |
Defaults to the size of the enclosing KlyFormField , or medium. Values: Xs, Sm, Md, Lg, Xl. |
|
Ui |
FileUploadUi |
Per-instance class overrides of the slots of the file upload, see FileUploadUi . They are merged after the theme and the Class parameter wins on the root slot. |
Events
| Name | Type | Description |
|---|---|---|
OnChange |
EventCallback |
Raised with the chosen files when the page is interactive. Setting it renders Blazor's InputFile ; read the files with e.GetMultipleFiles() or e.File . Without it, the input is a plain one, which is what static forms need. |
Ui slots
Ui takes a FileUploadUi with one class string per slot, merged after the theme.
| Slot | Styles |
|---|---|
Root |
Classes merged into the root slot of KlyFileUpload , after the theme and the app configuration. |
Dropzone |
Classes merged into the dropzone slot of KlyFileUpload , after the theme and the app configuration. |
Icon |
Classes merged into the icon slot of KlyFileUpload , after the theme and the app configuration. |
Label |
Classes merged into the label slot of KlyFileUpload , after the theme and the app configuration. |
Description |
Classes merged into the description slot of KlyFileUpload , after the theme and the app configuration. |
Files |
Classes merged into the files slot of KlyFileUpload , after the theme and the app configuration. |
File |
Classes merged into the file slot of KlyFileUpload , after the theme and the app configuration. |
FileName |
Classes merged into the fileName slot of KlyFileUpload , after the theme and the app configuration. |
FileSize |
Classes merged into the fileSize slot of KlyFileUpload , after the theme and the app configuration. |
Remove |
Classes merged into the remove slot of KlyFileUpload , after the theme and the app configuration. |
Error |
Classes merged into the error slot of KlyFileUpload , after the theme and the app configuration. |