File Configuration
FileConfig customizes how KraftAdmin validates and accepts uploaded files.
It is used together with @KraftAdminField when the field uses one of the media input types such as:
FormInputType.FILEFormInputType.IMAGEFormInputType.VIDEOFormInputType.AUDIOFormInputType.DOCUMENT
@KraftAdminField(
inputType = FormInputType.FILE,
fileConfig = FileConfig(...)
) If no FileConfig is provided, KraftAdmin uses sensible defaults.
Default Configuration
@KraftAdminField(
inputType = FormInputType.FILE,
fileConfig = FileConfig()
) Default values:
| Property | Default |
|---|---|
multiple | false |
maxFiles | 1 |
allowedExtensions | Any |
allowedMimeTypes | Any |
minSizeBytes | 0 |
maxSizeBytes | 10 MB |
Multiple Uploads
Allow uploading more than one file.
@KraftAdminField(
inputType = FormInputType.FILE,
fileConfig = FileConfig(
multiple = true,
maxFiles = 5
)
)
var documents: List<String> = emptyList() Maximum Files
Limit how many files may be uploaded.
fileConfig = FileConfig(
multiple = true,
maxFiles = 10
) Allowed Extensions
Restrict uploads by file extension.
fileConfig = FileConfig(
allowedExtensions = [
FileExtension.JPG,
FileExtension.PNG,
FileExtension.WEBP
]
) Supported extensions include:
Images
- JPG
- JPEG
- PNG
- GIF
- WEBP
- SVG
Documents
- DOC
- DOCX
- TXT
- CSV
- XLS
- XLSX
- PPT
- PPTX
Video
- MP4
- WEBM
- MOV
Audio
- MP3
- WAV
- OGG
- AAC
- FLAC
Archives
- ZIP
- RAR
- GZ
Allowed MIME Types
Validate uploaded files using MIME types.
fileConfig = FileConfig(
allowedMimeTypes = [
MimeType.IMAGE_JPEG,
MimeType.IMAGE_PNG
]
) This provides stronger validation than relying on file extensions alone.
Minimum File Size
Reject files smaller than a specified size.
fileConfig = FileConfig(
minSizeBytes = 1024
) Only files larger than 1 KB are accepted.
Maximum File Size
Restrict the maximum upload size.
fileConfig = FileConfig(
maxSizeBytes = 20 * 1024 * 1024
) The above example allows uploads up to 20 MB.
Image Upload Example
@KraftAdminField(
label = "Profile Photo",
inputType = FormInputType.IMAGE,
fileConfig = FileConfig(
allowedExtensions = [
FileExtension.JPG,
FileExtension.PNG,
FileExtension.WEBP
],
maxSizeBytes = 5 * 1024 * 1024
)
)
var avatar: String? = null PDF Upload Example
@KraftAdminField(
label = "Resume",
inputType = FormInputType.FILE,
fileConfig = FileConfig(
allowedExtensions = [
FileExtension.PDF
],
allowedMimeTypes = [
MimeType.PDF
]
)
)
var resume: String? = null Multiple Document Upload
@KraftAdminField(
label = "Attachments",
inputType = FormInputType.FILE,
fileConfig = FileConfig(
multiple = true,
maxFiles = 10,
maxSizeBytes = 50 * 1024 * 1024
)
)
var attachments: List<String> = emptyList() Best Practices
Always validate both file extensions and MIME types. Extensions are easy to rename, while MIME types provide an additional layer of verification.
Use multiple = true only when the field represents a collection of files.
Never rely solely on client-side validation. KraftAdmin validates uploads on the server, but uploaded files should still be scanned and stored securely.
FileConfig Properties
| Property | Description |
|---|---|
multiple | Allow multiple uploads |
maxFiles | Maximum number of uploaded files |
allowedExtensions | Allowed file extensions |
allowedMimeTypes | Allowed MIME types |
minSizeBytes | Minimum upload size |
maxSizeBytes | Maximum upload size |
See Also
- Image
- File
- Video
- Audio
- Document