Skip to content

Inputs

FileInput

Bit.BlazorUI

BitFileInput lets the user hand files to the app - through the file dialog, by dropping them onto it or by pasting them - without uploading anything. Each file surfaces in C# as a BitFileInputInfo (name, size, type, folder and, on request, image dimensions) and is validated on arrival against size, total size, type, count, duplicate and custom rules; a rejected file stays in the list with its reason. Content is read on demand, as a byte array or a stream. A drop zone panel, thumbnails, type glyphs, remove buttons, templates and screen reader announcements are built in. To send files to a server, use BitFileUpload.

Events, API & validation

The change events and reading the content, label and file templates, driving the component from code, its accessibility, and validating the selection inside an EditForm.

Events

OnFilesChange fires with all the files after every change, OnInvalid right after it with only the invalid ones, and OnRemove once per removed file. The inherited OnChange reports the Value alone - the names of the valid files - and only when it moved. Content is never loaded eagerly: ReadContentAsync reads one file, or every valid file when called without one.



Selected files:

Templates

LabelTemplate replaces the browse button and FileViewTemplate each file item. A template replaces real controls, so use real buttons in it to keep the keyboard working, and Classes.Dragging to give a custom label its drag feedback. For a ready-made drop panel, see Drop zone.

Public API

Browse, Reset (silent) and RemoveFile (one file or all, raising the events) drive the component from code, and Files and InputId expose its state. OpenReadStreamAsync reads a file in chunks: the way to handle large files, and on Blazor Server anything over SignalR's 32 KB message limit. HideLabel hides the browse button here; the drop indicator moves onto the component.



0 file(s) in the list.

Accessibility

The browse and remove buttons are real buttons with focus rings, and the focus survives a removal. The list is named by FileListAriaLabel, each remove button names its file and is described by its error, and a live region announces every change (AnnouncementProvider). AriaLabel names the browse button when its text says too little.

Up to 1 MB each. Tab to the button, then press Enter or Space.

Validation

Inside an EditForm the file input is a form field like any other. Its Value is the names of the valid files joined with commas (report.pdf,photo.png), or null while it holds none, so a [Required] or a [RegularExpression] on the bound property validates the selection. A file the component's own rules reject (here, anything but a PDF or a Word document) is not part of the value, and while the value is invalid the browse button turns to the error color. Required also marks the hidden input the value travels in, which Name makes readable by a plain form post. A browser cannot be handed files from code, but emptying the bound value clears the selection.

ShowValidationMessage shows the messages of the form under the browse button, where ErrorMessage goes, so the form below needs no ValidationMessage component. The button is described by them through aria-describedby, so they are read out when it takes the focus, and an ErrorMessage of the app's own wins the place while it is set. They are about the selection as a whole: a file the component rejects on its own still says why in the list. They are not announced as they appear: a submit validates every field at once, and a message from each of them read out in a row would bury the form under its own errors. A BitParams holding a BitFileInputParams with ShowValidationMessage turns it on for every file input inside it.




Not every rejection comes from an EditForm. Invalid forces the error look and the aria-invalid of the browse button on a selection something else refused - a server, a rule spanning two fields - and ErrorMessage adds the line that says why, right under the button and ahead of the Description. It is about the selection as a whole: a file the component rejects itself still says why in the list. A message implies Invalid, so the flag on its own is for marking a file input whose explanation lives somewhere else. The button is described by the line, and a live region of its own, apart from the one that announces the selection, says it the moment it appears. ErrorMessageTemplate puts arbitrary markup there instead. A template has no text a screen reader can be handed, so it is announced as a bare "Invalid input" unless ErrorMessageAriaLabel says what it shows.

Pick two files to see the message.


Your storage is full. Free up space(opens in a new tab)
Your storage is full. Free up space to upload more.
Your storage is full. Free up space to upload more.

Feedback

Found a mistake, a gap, or something that could be clearer? Every page and every component is one click from its source.