File Input
The FileInput component is an enhanced wrapper for <input type="file">. It supports single/multiple uploads, size options, and includes a built-in automatic preview feature (Phase 4B).
Bootstrap Reference
Quick Start
Visual Examples & Use Cases
1. Multiple Files & Sizing
Allow uploading multiple files at once.
Code & Output
2. Automatic Image Preview (✨ New in Phase 4B)
FastStrap includes a lightweight JavaScript snippet that can automatically show a preview of selected images before upload.
Set preview_id="auto" to have FastStrap handle everything for you.
Code & Output
Parameter Reference
| Param | Type | HTML Attribute | Description |
|---|---|---|---|
name |
str |
name="..." |
Form field name. |
label |
str |
<label> |
Input label. |
multiple |
bool |
multiple |
Allow selecting multiple files. |
accept |
str |
accept="..." |
File type filter (e.g. image/*, .pdf). |
preview_id |
str |
- | ID of element to display preview in. Use "auto" for automatic generation. |
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 |
Example
Basic:
FileInput("upload", label="Upload file")
With preview:
FileInput("avatar", label="Avatar", accept="image/*", preview_id="auto")
Multiple:
FileInput("docs", label="Documents", multiple=True)
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 | |