FileInput

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.

Usage

Basic
Select file(s) to see them in the component.

<BitFileInput Label="Browse or drop a file" />
Multiple
Multiple files can be selected.

<BitFileInput Label="Browse or drop files" Multiple />
AutoReset
Automatically resets the BitFileInput state each time before browsing files.

<BitFileInput Label="Browse or drop a file" AutoReset />
Append
When selected, additional files will be appended to the existing list without overwriting previous selections.

<BitFileInput Label="Browse or drop a file" Append />
MaxSize
The file size can be limited using the MaxSize parameter (1 MB in this example).

<BitFileInput Label="Browse or drop a file" MaxSize="1024 * 1024 * 1" />
AllowedExtensions
Limits file browsing by the provided file extensions.

<BitFileInput Label="Browse or drop a file" AllowedExtensions="@([".gif",".jpg",".png",".bmp"])" />
Removable
Enables the remove functionality of the BitFileInput.

<BitFileInput Label="Browse or drop a file" ShowRemoveButton />
Events
Different events can be configured for file selection.

Selected files:

<BitFileInput Label="Select or drag and drop files" OnChange="@HandleOnChange" />

<div>Selected files:</div>
@foreach (var file in selectedFiles)
{
    <div>@file.Name (@FileSizeHumanizer.Humanize(file.Size))</div>
}
@code {
    private BitFileInputInfo[] selectedFiles = [];
    
    private void HandleOnChange(BitFileInputInfo[] files)
    {
        selectedFiles = files;
    }
}
                    
Templates
The BitFileInput can be further customized using templates.

Drag and drop or
Browse files
<style>
    .browse-file {
        border: 1px solid #D2D2D7;
        border-radius: 2px;
        padding: 24px;
        width: 420px;
        height: 200px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        gap: 50px;
        cursor: pointer;
    }

    .browse-file-header {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        font-size: 16px;
    }

    .browse-file-header i {
        font-size: 24px;
        font-weight: 700;
        color: #0072CE;
    }

    .browse-file-header strong {
        color: #0072CE;
    }

    .browse-file-footer {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        font-size: 12px;
        color: #78787D;
    }

    .file-list {
        border: 1px solid #D2D2D7;
        border-radius: 2px;
        padding: 24px;
        width: 420px;
        height: 200px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
    }

    .file-list-header {
        display: flex;
        flex-direction: column;
        gap: 8px;
    }

    .file-info {
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .file-info-main {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        gap: 12px;
    }

    .file-info-main i {
        font-size: 24px;
    }

    .file-info-data {
        width: 275px;
    }

    .file-info-btns {
        display: flex;
        justify-content: space-between;
        gap: 8px;
    }

    .file-info-btns i {
        display: block;
        cursor: pointer;
    }

    .file-info-btns .remove-ico {
        color: #F9423A;
    }

    .file-info-e-msg {
        margin-top: 12px;
        color: #F9423A;
    }

    .file-list-footer {
        font-size: 12px;
        color: #78787D;
    }
</style>

<BitFileInput @ref="bitFileInput" Multiple
              MaxSize="1024 * 1024 * 2"
              AllowedExtensions="@([".jpg", ".jpeg", ".png", ".bmp"])">
    <LabelTemplate>
        @if (bitFileInput.Files?.Any() is not true)
        {
            <div class="browse-file" @onclick="() => bitFileInput.Browse()">
                <div class="browse-file-header">
                    <i class="bit-icon bit-icon--CloudUpload" />
                    <div>
                        Drag and drop or
                    </div>
                    <div>
                        <strong>
                            Browse files
                        </strong>
                    </div>
                </div>

                <div class="browse-file-footer">
                    <div>Max file size: 2 MB</div>
                    <div>Supported file types: jpg, jpeg, png, bmp</div>
                </div>
            </div>
        }
    </LabelTemplate>
    <FileViewTemplate Context="file">
        @if (!string.IsNullOrEmpty(file.Name))
        {
            <div class="file-list">
                <div class="file-list-header">
                    <div class="file-info">
                        <div class="file-info-main">
                            <i class="bit-icon bit-icon--Page" />
                            <div class="file-info-data">
                                <div class="file-info-name">
                                    @file.Name
                                </div>
                                <div>
                                    @FileSizeHumanizer.Humanize(file.Size)
                                </div>
                            </div>
                        </div>

                        <div class="file-info-btns">
                            <i class="bit-icon bit-icon--Cancel remove-ico"
                                @onclick="() => bitFileInput.RemoveFile(file)" />
                        </div>
                    </div>

                    @if (!file.IsValid)
                    {
                        <div class="file-info-e-msg">@file.Message</div>
                    }
                </div>

                <div class="file-list-footer">
                    <div>Max file size: 2 MB</div>
                    <div>Supported file types: jpg, jpeg, png, bmp</div>
                </div>
            </div>
        }
    </FileViewTemplate>
</BitFileInput>
@code {
    private BitFileInput bitFileInput = default!;
}
                    
Public API
Use custom methods to interact with the file input.


<BitFileInput @ref="publicApiFileInput" HideLabel />

<BitButton OnClick="() => publicApiFileInput.Browse()">Browse file</BitButton>
<BitButton OnClick="() => publicApiFileInput.Reset()">Reset</BitButton>
@code {
    private BitFileInput publicApiFileInput = default!;
}
                    

API

BitFileInput parameters
Name
Type
Default value
Description
Accept string? null Specifies the accepted file types using MIME types or file extensions (e.g., "image/*", ".pdf,.doc"). This value is applied to the HTML input element's accept attribute.
AllowedExtensions IReadOnlyCollection<string> ["*"] Specifies the allowed file extensions for validation (e.g., [".jpg", ".png", ".pdf"]). Use ["*"] to allow all file types. Files not matching these extensions will be marked as invalid.
Append bool false When enabled, newly selected files are appended to the existing file list instead of replacing it.
AutoReset bool false When enabled, the file input is automatically reset (cleared) before opening the file browser dialog. This allows selecting the same file multiple times consecutively.
HideFileList bool false When enabled, the file list displaying selected files is hidden from the UI.
HideLabel bool false When enabled, the default browse button label is hidden from the UI.
Label string Browse The text displayed on the browse button. Defaults to "Browse" if not specified.
LabelTemplate RenderFragment? null A custom Razor template for the browse button area, allowing full customization of the file selection UI.
MaxSize long 0 The 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 The error message displayed when a file exceeds the maximum size limit. Defaults to "The file size is larger than the max size" if not specified.
Multiple bool false When enabled, allows selecting multiple files simultaneously through the file browser dialog.
NotAllowedExtensionErrorMessage string File type not allowed The error message displayed when a file's extension is not in the allowed extensions list. Defaults to "The file type is not allowed" if not specified.
OnChange EventCallback<BitFileInputInfo[]> Callback invoked when the file selection changes. Receives an array of BitFileInputInfo objects representing all selected files.
ShowRemoveButton bool false When enabled, displays a remove button next to each file in the file list, allowing users to individually remove files from the selection.
FileViewTemplate RenderFragment<BitFileInputInfo>? null A custom Razor template for rendering individual file items in the file list. Receives a BitFileInputInfo context for each file.
BitFileInput public members
Name
Type
Default value
Description
Files BitFileInputInfo[]? null Gets a read-only list of all currently selected files with their metadata and validation status.
InputId string? Gets the unique identifier of the underlying HTML file input element.
RemoveFile (BitFileInputInfo? fileInfo = null) => void Removes one or more files from the selected files list. If fileInfo is null, all files will be removed.
Browse Task Programmatically opens the file browser dialog, allowing users to select files. If AutoReset is enabled, the input is reset before opening the dialog.
Reset Task Clears all selected files and resets the file input to its initial state.
BitComponentBase parameters
Name
Type
Default value
Description
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.
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.
BitComponentBase public members
Name
Type
Default value
Description
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.
BitFileInputInfo properties
Represents metadata and validation information for a file selected through BitFileInput.
Name
Type
Default value
Description
ContentType String string.Empty Gets or sets the MIME content type of the selected file (e.g., "image/png", "application/pdf").
Name String string.Empty Gets or sets the name of the selected file, including its extension.
Size long Gets or sets the size of the selected file in bytes.
FileId String string.Empty Gets or sets the unique identifier (GUID) assigned to the selected file.
Index int Gets or sets the zero-based index of the file in the selection list.
IsValid bool true Gets or sets a value indicating whether the file passed all validation checks (size constraints and allowed extensions).
Message string? null Gets or sets the validation error message if the file failed validation. This property is null when the file is valid.
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.

Feedback

You can give us your feedback through our GitHub repo by filing a new Issue or starting a new Discussion.

Or you can review / edit this page on GitHub.

Or you can review / edit this component on GitHub.
  • On this page