> For the complete documentation index, see [llms.txt](https://docs.forms.md/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.forms.md/input-types/file-input.md).

# File input

<figure><img src="/files/b4sUAGE0u7OvLDIBojVE" alt=""><figcaption><p>File input</p></figcaption></figure>

Use the `fileInput()` function to create file upload inputs in your forms. It uses the HTML `<input type="file">` element which allows users to upload files from their device.

***

## Basic usage

```javascript
import { Composer } from "formsmd";

const composer = new Composer({
  id: "my-form"
});

composer.fileInput("document", {
  question: "Upload your document"
});
```

Generates the following Markdown-like syntax:

```
#! id = my-form

document = FileInput(
  | question = Upload your document
)
```

### Required

Add the `required` parameter to make the field mandatory:

```javascript
composer.fileInput("document", {
  question: "Upload your document",
  required: true
});
```

Generates the following Markdown-like syntax:

```
document* = FileInput(
  | question = Upload your document
)
```

***

## Function overview

The following is the overview of the function:

```typescript
fileInput(name: string, params: object)
```

### Arguments

| Name     | Type     | Description                                                                                                                                                     |
| -------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name`   | `string` | A unique name for your form field that you'll use to identify the user's response.<mark style="color:red;">\*</mark>                                            |
| `params` | `object` | An object containing all the configuration parameters for your file input field (see the [parameters section](#parameters) below for the full list of options). |

{% hint style="danger" %} <mark style="color:red;">\*</mark>Avoid values for the `name` argument which may be the names of HTML attributes, such as `"name"`, `"role"`, `"id"`, etc. This is because by default, the form's template string is first sanitized using [DOMPurify](https://github.com/cure53/DOMPurify), and these values may be removed to prevent DOM clobbering.
{% endhint %}

***

## Parameters

### Shared parameters

These parameters are common to all form fields:

| Name                  | Type                                            | Description                                                                                                                                                                                                                                                                             |
| --------------------- | ----------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `question` (required) | `string`                                        | The main question or label of the form field.                                                                                                                                                                                                                                           |
| `required`            | `true` (`boolean`)                              | When set, the field becomes required.                                                                                                                                                                                                                                                   |
| `description`         | `string`                                        | Any extra information that the user may need to fill out the form. Appears right below the question.                                                                                                                                                                                    |
| `fieldSize`           | `"sm"`                                          | When set to `"sm"`, the font sizes of the question, description, and answer are made smaller.                                                                                                                                                                                           |
| `labelStyle`          | `"classic"`                                     | When set to `"classic"`, the question and description of the form field are made smaller.                                                                                                                                                                                               |
| `subfield`            | `true` (`boolean`)                              | When set, the question and description of the form field are made smaller. Functionally the same as setting `labelStyle` to `"classic"`.                                                                                                                                                |
| `disabled`            | `true` (`boolean`)                              | When set, the input is disabled.                                                                                                                                                                                                                                                        |
| `autofocus`           | `true` (`boolean`)                              | When set, the input will be automatically focused when the parent slide becomes active, or immediately after page load.                                                                                                                                                                 |
| `id`                  | `string`                                        | The `id` attribute of the form field container.                                                                                                                                                                                                                                         |
| `classNames`          | `string[]`                                      | The CSS class names of the form field. [See the available CSS utility classes](/content/css-utility-classes.md).                                                                                                                                                                        |
| `attrs`               | `Array<{ name: string, value: string }>`        | Other HTML attributes of the form field. Each attribute has a `name` and `value` property.                                                                                                                                                                                              |
| `displayCondition`    | `{ dependencies: string[], condition: string }` | Controls when the field is shown. The `dependencies` lists the fields to watch, and `condition` is the expression that must be true to show the field. The `condition` must be a valid [Nunjucks](https://mozilla.github.io/nunjucks/) expression. [See example](#conditional-display). |

### File input specific parameters

| Name          | Type     | Description                                                                                                                  |
| ------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------- |
| `sizeLimit`   | `number` | Maximum file size in MB. Defaults to `10`. Client-side check only, make sure to also validate on the server.                 |
| `imageOnly`   | `true`   | When set, only image files are accepted. Client-side check only, make sure to also validate on the server.                   |
| `currentFile` | `string` | The current file that exists in the database. Use a URL for best results, for example, `"https://example.s3.com/image.png"`. |

***

## Examples

### File input with size limit

```javascript
composer.fileInput("resume", {
  question: "Upload your resume",
  description: "PDF format preferred",
  sizeLimit: 5,
  required: true
});
```

Generates the following Markdown-like syntax:

```
resume* = FileInput(
  | question = Upload your resume
  | description = PDF format preferred
  | sizelimit = 5
)
```

### Image-only file input

```javascript
composer.fileInput("profilePicture", {
  question: "Profile picture",
  description: "Upload a clear photo of yourself",
  imageOnly: true,
  sizeLimit: 2,
  required: true
});
```

Generates the following Markdown-like syntax:

```
profilePicture* = FileInput(
  | question = Profile picture
  | description = Upload a clear photo of yourself
  | imageonly
  | sizelimit = 2
)
```

### File input with existing file

```javascript
composer.fileInput("document", {
  question: "Update your document",
  currentFile: "https://example.s3.com/previous-doc.pdf"
});
```

Generates the following Markdown-like syntax:

```
document = FileInput(
  | question = Update your document
  | currentfile = https://example.s3.com/previous-doc.pdf
)
```

### Styled file input with custom attributes

Add CSS classes and other HTML attributes using the `classNames` and `attrs` parameters. Please note, these class names and attributes are added to the `<div>` or `<fieldset>` container that contains the actual input field(s).

```javascript
composer.fileInput("attachment", {
  question: "Upload attachment",
  classNames: ["col-6", "xs:col-6"],
  attrs: [
    { name: "style", value: "font-size: 18px;" }
  ]
});
```

Generates the following Markdown-like syntax:

```
[.col-6 .xs:col-6 style="font-size: 18px;"]
attachment = FileInput(
  | question = Upload attachment
)
```

Please [see the available CSS utility classes](/content/css-utility-classes.md).

### Conditional display

Conditionally show or hide a file input field using the `displayCondition` parameter. It works as follows:

* `dependencies` lists the fields to watch.
* `condition` is the expression that must be true to show the field. This must be a valid [Nunjucks](https://mozilla.github.io/nunjucks/) expression.

For instance, in the example below, the document upload field will only show up if the user indicates they have additional documents.

```javascript
composer.choiceInput("hasDocument", {
  question: "Do you have any supporting document?",
  required: true,
  choices: ["Yes", "No"]
})

composer.fileInput("supportingDoc", {
  question: "Upload supporting document",
  description: "You can upload PDF, image, or a Word document",
  displayCondition: {
    dependencies: ["hasDocument"],
    condition: "hasDocument == 'Yes'"
  }
});
```

Generates the following Markdown-like syntax:

```
hasDocument* = ChoiceInput(
  | question = Do you have any supporting document?
  | choices = Yes, No
)

::: [{$ hasDocument $}]
{% if hasDocument == "Yes" %}
supportingDoc = FileInput(
  | question = Upload supporting document
  | description = You can upload PDF, image, or a Word document
)
{% endif %}
:::
```
