Components

File upload

Help users select and upload a file.

<div class="govuk-form-group">
  <label class="govuk-label" for="file-upload-1">
    Upload a file
  </label>
  <input class="govuk-file-upload" id="file-upload-1" name="file-upload-1" type="file">
</div>
Nunjucks macro options

Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.

Some options are required for the macro to work; these are marked as "Required" in the option description.

If you're using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against cross-site scripting exploits.

Primary options
Name Type Description
name string Required. The name of the input, which is submitted with the form data.
id string Required. The id of the input
value string Optional initial value of the input
label object Required. Options for the label component. See label.
hint object Options for the hint component. See hint.
errorMessage object Options for the errorMessage component. See errorMessage.
formGroup object Options for the form-group wrapper See formGroup.
classes string Classes to add to the file upload component.
attributes object HTML attributes (for example data attributes) to add to the file upload component.
Options for formGroup
Name Type Description
classes string Optional classes to add to the form group (e.g. to show error state for the whole group)
Options for label
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the label. If `html` is provided, the `text` argument will be ignored.
html string Required. If `text` is set, this is not required. HTML to use within the label. If `html` is provided, the `text` argument will be ignored.
for string Required. The value of the for attribute, the id of the input the label is associated with.
isPageHeading boolean Whether the label also acts as the heading for the page.
classes string Classes to add to the label tag.
attributes object HTML attributes (for example data attributes) to add to the label tag.
Options for hint
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the hint. If `html` is provided, the `text` argument will be ignored.
html string Required. If `text` is set, this is not required. HTML to use within the hint. If `html` is provided, the `text` argument will be ignored.
id string Required. Optional id attribute to add to the hint span tag.
classes string Classes to add to the hint span tag.
attributes object HTML attributes (for example data attributes) to add to the hint span tag.
{% from "file-upload/macro.njk" import govukFileUpload %}

{{ govukFileUpload({
  id: "file-upload-1",
  name: "file-upload-1",
  label: {
    text: "Upload a file"
  }
}) }}

When to use this component

You should only ask users to upload something if it’s critical to the delivery of your service.

How it works

There are 2 ways to use the file upload component. You can use HTML or, if you’re using Nunjucks or the GOV.UK Prototype Kit, you can use the Nunjucks macro.

<div class="govuk-form-group">
  <label class="govuk-label" for="file-upload-1">
    Upload a file
  </label>
  <input class="govuk-file-upload" id="file-upload-1" name="file-upload-1" type="file">
</div>
Nunjucks macro options

Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.

Some options are required for the macro to work; these are marked as "Required" in the option description.

If you're using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against cross-site scripting exploits.

Primary options
Name Type Description
name string Required. The name of the input, which is submitted with the form data.
id string Required. The id of the input
value string Optional initial value of the input
label object Required. Options for the label component. See label.
hint object Options for the hint component. See hint.
errorMessage object Options for the errorMessage component. See errorMessage.
formGroup object Options for the form-group wrapper See formGroup.
classes string Classes to add to the file upload component.
attributes object HTML attributes (for example data attributes) to add to the file upload component.
Options for formGroup
Name Type Description
classes string Optional classes to add to the form group (e.g. to show error state for the whole group)
Options for label
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the label. If `html` is provided, the `text` argument will be ignored.
html string Required. If `text` is set, this is not required. HTML to use within the label. If `html` is provided, the `text` argument will be ignored.
for string Required. The value of the for attribute, the id of the input the label is associated with.
isPageHeading boolean Whether the label also acts as the heading for the page.
classes string Classes to add to the label tag.
attributes object HTML attributes (for example data attributes) to add to the label tag.
Options for hint
Name Type Description
text string Required. If `html` is set, this is not required. Text to use within the hint. If `html` is provided, the `text` argument will be ignored.
html string Required. If `text` is set, this is not required. HTML to use within the hint. If `html` is provided, the `text` argument will be ignored.
id string Required. Optional id attribute to add to the hint span tag.
classes string Classes to add to the hint span tag.
attributes object HTML attributes (for example data attributes) to add to the hint span tag.
{% from "file-upload/macro.njk" import govukFileUpload %}

{{ govukFileUpload({
  id: "file-upload-1",
  name: "file-upload-1",
  label: {
    text: "Upload a file"
  }
}) }}

Research on this component

Read a blog post about design tips for helping users upload things.

Get in touch

If you’ve got a question, idea or suggestion share it in #govuk-design-system on cross-government Slack (open in app) or email the Design System team on govuk-design-system-support@digital.cabinet-office.gov.uk

Discuss ‘File upload’ on GitHub