File Input
The FileInput component provides a styled file upload control with optional image preview support.
Quick Start
Usage Scenarios
With Image Preview
Multiple Files
With Accept Filter
Parameter Reference
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str |
Required | Input name attribute |
label |
str \| None |
None |
Label text |
multiple |
bool |
False |
Allow selecting multiple files |
disabled |
bool |
False |
Disable the input |
required |
bool |
False |
Mark as required |
accept |
str \| None |
None |
File types to accept (e.g. "image/*", ".pdf") |
size |
str \| None |
None |
Control size (sm, lg) |
file_id |
str \| None |
None |
ID for the input |
input_cls |
str |
"" |
Additional classes for input element |
label_cls |
str |
"" |
Additional classes for label element |
helper_text |
str \| None |
None |
Help text displayed below input |
preview_id |
str \| None |
None |
ID of an img element for preview. Use "auto" to create one automatically. |
preview_img_cls |
str |
"img-thumbnail mt-2" |
Classes for the preview image |
preview_max_height |
str |
"200px" |
Max height for preview image |
**kwargs |
Any |
- | Additional HTML attributes |
Accessibility
- Labels are associated with inputs via
forandidattributes. - Required fields use the
requiredattribute. - Preview images include
alt="File preview".
API Reference
faststrap.components.forms.file.FileInput(name, *, label=None, multiple=False, disabled=False, required=False, accept=None, size=None, file_id=None, input_cls='', label_cls='', helper_text=None, preview_id=None, preview_img_cls='img-thumbnail mt-2', preview_max_height='200px', **kwargs)
Bootstrap File Input component.
A styled file upload control with optional image preview support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Input name attribute |
required |
label
|
str | None
|
Label text |
None
|
multiple
|
bool
|
Allow selecting multiple files |
False
|
disabled
|
bool
|
Disable the input |
False
|
required
|
bool
|
Mark as required |
False
|
accept
|
str | None
|
File types to accept (e.g. "image/*", ".pdf") |
None
|
size
|
SizeType | None
|
Control size (sm, lg) |
None
|
file_id
|
str | None
|
ID for the input (auto-generated from name if not provided) |
None
|
input_cls
|
str
|
Additional classes for input element |
''
|
label_cls
|
str
|
Additional classes for label element |
''
|
helper_text
|
str | None
|
Help text displayed below input |
None
|
preview_id
|
str | None
|
ID of an img element to show preview in. If "auto", creates a preview area automatically. |
None
|
preview_img_cls
|
str
|
Classes for the preview image |
'img-thumbnail mt-2'
|
preview_max_height
|
str
|
Max height for preview image |
'200px'
|
**kwargs
|
Any
|
Additional HTML attributes (hx-*, etc.) |
{}
|
Returns:
| Type | Description |
|---|---|
Div
|
FastHTML Div element with file input structure |
Examples:
Basic:
With preview:
Multiple:
Source code in src/faststrap/components/forms/file.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |