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.FILE
  • FormInputType.IMAGE
  • FormInputType.VIDEO
  • FormInputType.AUDIO
  • FormInputType.DOCUMENT
@KraftAdminField(
    inputType = FormInputType.FILE,
    fileConfig = FileConfig(...)
)
ℹ️ Info

If no FileConfig is provided, KraftAdmin uses sensible defaults.


Default Configuration

@KraftAdminField(
    inputType = FormInputType.FILE,
    fileConfig = FileConfig()
)

Default values:

PropertyDefault
multiplefalse
maxFiles1
allowedExtensionsAny
allowedMimeTypesAny
minSizeBytes0
maxSizeBytes10 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

  • PDF
  • 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

💡 Tip

Always validate both file extensions and MIME types. Extensions are easy to rename, while MIME types provide an additional layer of verification.

💡 Tip

Use multiple = true only when the field represents a collection of files.

⚠️ Warning

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

PropertyDescription
multipleAllow multiple uploads
maxFilesMaximum number of uploaded files
allowedExtensionsAllowed file extensions
allowedMimeTypesAllowed MIME types
minSizeBytesMinimum upload size
maxSizeBytesMaximum upload size

See Also

  • Image
  • File
  • Video
  • Audio
  • Document