Accent color
# FileInput ## Description BitFileInput component wraps the HTML file input element and allows the user to select file(s) without uploading them. The selected files are returned to the C# context for further processing. It provides several options including single or multiple file selection, drag and drop support, file size validation, and file extension filtering. ## Parameters | Name | Type | Default Value | Description | | :--- | :--- | :------------ | :---------- | | Accept | `string?` | null | Accepted file types for the file browser using MIME types or file extensions (e.g., "image/*", ".pdf,.doc"). Applied to the underlying HTML input element's accept attribute. | | AllowedExtensions | `IReadOnlyCollection<string>` | ["*"] | Allowed file extensions for validation purposes (e.g., [".jpg", ".png", ".pdf"]). Use ["*"] to allow all file types. Files not matching these extensions will be marked as invalid. | | Append | `bool` | false | Whether to append newly selected files to the existing file list instead of replacing it. | | AutoReset | `bool` | false | Whether the file input is automatically reset (cleared) before opening the file browser dialog, allowing the same file to be selected multiple times consecutively. | | FileViewTemplate | `RenderFragment<BitFileInputInfo>?` | null | Custom Razor template for rendering individual file items in the file list. Receives a BitFileInputInfo context for each file. | | HideFileList | `bool` | false | Whether to hide the file list that displays the selected files in the UI. | | HideLabel | `bool` | false | Whether to hide the default browse button label from the UI. | | Label | `string` | Browse | The text displayed on the browse button. Defaults to "Browse". | | LabelTemplate | `RenderFragment?` | null | Custom Razor template for the browse button area, allowing full customization of the file selection trigger UI. | | MaxSize | `long` | 0 | Maximum allowed file size in bytes for validation. Files exceeding this size will be marked as invalid. Set to 0 for no size limit. | | MaxSizeErrorMessage | `string?` | null | Custom error message displayed when a file exceeds the maximum size limit. Defaults to "The file size is larger than the max size". | | Multiple | `bool` | false | Whether to allow selecting multiple files simultaneously through the file browser dialog. | | NotAllowedExtensionErrorMessage | `string` | File type not allowed | Custom error message displayed when a file's extension is not in the allowed extensions list. Defaults to "The file type is not allowed". | | OnChange | `EventCallback<BitFileInputInfo[]>` | | Callback invoked when the file selection changes, providing an array of BitFileInputInfo representing all selected files. | | RemoveButtonIcon | `BitIconInfo?` | null | Gets or sets the remove button icon using custom CSS classes for external icon libraries. Takes precedence over RemoveButtonIconName when both are set. | | RemoveButtonIconName | `string?` | Delete | Gets or sets the name of the remove button icon from the built-in Fluent UI icons. | | ShowRemoveButton | `bool` | false | Whether to display a remove button next to each file in the file list, allowing individual file removal. | | AriaLabel | `string?` | null | Gets or sets the accessible label for the component, used by assistive technologies. | | Class | `string?` | null | Gets or sets the CSS class name(s) to apply to the rendered element. | | Dir | `BitDir?` | null | Gets or sets the text directionality for the component's content. | | ForceAnimation | `bool` | false | Gets or sets a value indicating whether the component's animations play at their full duration even when reduced motion is requested. | | HtmlAttributes | `Dictionary<string, object>` | new Dictionary<string, object>() | Captures additional HTML attributes to be applied to the rendered element, in addition to the component's parameters. | | Id | `string?` | null | Gets or sets the unique identifier for the component's root element. | | IsEnabled | `bool` | true | Gets or sets a value indicating whether the component is enabled and can respond to user interaction. | | Style | `string?` | null | Gets or sets the CSS style string to apply to the rendered element. | | TabIndex | `string?` | null | Gets or sets the tab order index for the component when navigating with the keyboard. | | Visibility | `BitVisibility` | BitVisibility.Visible | Gets or sets the visibility state (visible, hidden, or collapsed) of the component. | ## Public Members | Name | Type | Default Value | Description | | :--- | :--- | :------------ | :---------- | | Files | `BitFileInputInfo[]?` | null | A read-only list of all currently selected files with their metadata, validation status, and content. | | InputId | `string?` | | The unique identifier of the underlying HTML file input element. | | RemoveFile | `(BitFileInputInfo? fileInfo = null) => void` | | Removes a specific file from the selected files list, or clears all files when no file is specified. | | Browse | `Task` | | Opens the file browser dialog programmatically, allowing users to select files. If AutoReset is enabled, the input is reset before opening. | | ReadContentAsync | `(BitFileInputInfo fileInfo) => Task` | | Reads the content of the specified file from the browser and populates its Content property with the byte array. Only reads valid and enabled files. | | Reset | `Task` | | Clears all selected files and resets the file input to its initial state. | | UniqueId | `Guid` | Guid.NewGuid() | Gets the readonly unique identifier for the component's root element, assigned when the component instance is constructed. | | RootElement | `ElementReference` | | Gets the reference to the root HTML element associated with this component. | ## Enums ### BitVisibility Enum | Name | Value | Description | | :--- | :--- | :---------- | | Visible | 0 | The content of the component is visible. | | Hidden | 1 | The content of the component is hidden, but the space it takes on the page remains (visibility:hidden). | | Collapsed | 2 | The component is hidden (display:none). | ### BitDir Enum | Name | Value | Description | | :--- | :--- | :---------- | | Ltr | 0 | Ltr (left to right) is to be used for languages that are written from the left to the right (like English). | | Rtl | 1 | Rtl (right to left) is to be used for languages that are written from the right to the left (like Arabic). | | Auto | 2 | Auto lets the user agent decide. It uses a basic algorithm as it parses the characters inside the element until it finds a character with a strong directionality, then applies that directionality to the whole element. | ## Sub Classes ### BitFileInputInfo Properties Represents metadata, validation state, and content of a file selected through BitFileInput. | Name | Type | Default Value | Description | | :--- | :--- | :------------ | :---------- | | ContentType | `String` | string.Empty | The MIME content type of the file (e.g., "image/png", "application/pdf"). | | Name | `String` | string.Empty | The name of the file including its extension (e.g., "document.pdf"). | | Size | `long` | | The size of the file in bytes. | | FileId | `String` | string.Empty | A unique identifier (GUID) assigned to the file upon selection, used to reference the file in JavaScript interop. | | Index | `int` | | The zero-based index of the file in the current selection list. | | IsValid | `bool` | true | Whether the file has passed all validation checks including size constraints and allowed extensions. | | Message | `string?` | null | The validation error message when the file has failed a validation check (e.g., size or extension). This is null when the file is valid. | | Content | `byte[]?` | null | The file content as a byte array, populated by calling ReadContentAsync. This is null by default and only loaded on demand. | ### BitIconInfo Properties | Name | Type | Default Value | Description | | :--- | :--- | :------------ | :---------- | | Name | `string?` | null | Gets or sets the name of the icon. | | BaseClass | `string?` | null | Gets or sets the base CSS class for the icon. For built-in Fluent UI icons, this defaults to "bit-icon". For external icon libraries like FontAwesome, you might set this to "fa" or leave empty. | | Prefix | `string?` | null | Gets or sets the CSS class prefix used before the icon name. For built-in Fluent UI icons, this defaults to "bit-icon--". For external icon libraries, you might set this to "fa-" or leave empty. | ## Examples \n**Basic**: ```razor ``` \n**Multiple**: ```razor ``` \n**AutoReset**: ```razor ``` \n**Append**: ```razor ``` \n**MaxSize**: ```razor ``` \n**AllowedExtensions**: ```razor ``` \n**Removable**: ```razor ``` \n**Events**: ```razor
Selected files:
@foreach (var file in selectedFiles) {
@file.Name (@FileSizeHumanizer.Humanize(file.Size)) @if (file.Content is not null) { - @file.Content.Length bytes loaded }
} ``` ```csharp private BitFileInput eventsFileInput = default!; private BitFileInputInfo[] selectedFiles = []; private async Task HandleOnChange(BitFileInputInfo[] files) { selectedFiles = files; foreach (var file in files) { if (file.IsValid && file.Content is null) { await eventsFileInput.ReadContentAsync(file); } } } ``` \n**Templates**: ```razor @if (bitFileInput.Files?.Any() is not true) {
Drag and drop or
Browse files
}
@if (!string.IsNullOrEmpty(file.Name)) {
@file.Name
@FileSizeHumanizer.Humanize(file.Size)
@if (!file.IsValid) {
@file.Message
}
}
``` ```csharp private BitFileInput bitFileInput = default!; ``` \n**Public API**: ```razor Browse file Reset ``` ```csharp private BitFileInput publicApiFileInput = default!; ``` \n**External Icons**: ```razor ```