Components

File upload

This guidance is for government teams that build online services. To find information and services for the public, go to GOV.UK.

WCAG 2.2

New WCAG 2.2 criteria affect this component

To use the ‘File upload’ and meet the new Web Content Accessibility Guidelines (WCAG) 2.2 criteria, make sure that users can successfully:

See the full list of components and patterns affected by WCAG 2.2.

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="fileUpload1" 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.
disabled boolean If true, file input will be disabled.
describedBy string One or more element IDs to add to the aria-describedby attribute, used to provide additional descriptive information for screenreader users.
label object Required. The label used by the file upload component. See label.
hint object Can be used to add a hint to the file upload component. See hint.
errorMessage object Can be used to add an error message to the file upload component. The error message component will not display if you use a falsy value for errorMessage, for example false or null. See errorMessage.
formGroup object Additional options for the form group containing the file upload component. 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 object
Name Type Description
classes string Classes to add to the form group (for example to show error state for the whole group).
attributes object HTML attributes (for example data attributes) to add to the form group.
beforeInput object Content to add before the input used by the file upload component. See formGroup beforeInput.
afterInput object Content to add after the input used by the file upload component. See formGroup afterInput.
Options for formGroup beforeInput object
Name Type Description
text string Required. Text to add before the input. If html is provided, the text option will be ignored.
html string Required. HTML to add before the input. If html is provided, the text option will be ignored.
Options for formGroup afterInput object
Name Type Description
text string Required. Text to add after the input. If html is provided, the text option will be ignored.
html string Required. HTML to add after the input. If html is provided, the text option will be ignored.
Options for label component
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 option 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 option will be ignored.
for string 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 component
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 option 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 option will be ignored.
id string 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 "govuk/components/file-upload/macro.njk" import govukFileUpload %}

{{ govukFileUpload({
  id: "file-upload-1",
  name: "fileUpload1",
  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

To upload a file, the user can either:

  • use the ‘Choose file’ button
  • ‘drag and drop’ a file into the file upload input area
WCAG 2.2

Do not use ‘drag and drop’ as the only way to upload files. You must provide another method, such as the ‘Choose file’ button. This is to comply with WCAG 2.2 success criterion 2.5.7 Dragging movements.

WCAG 2.2

Make sure users can easily reuse a previously uploaded file within a single journey, unless doing so would be a major security or privacy concern.

For example, a user might need to upload a photo of their driving licence to prove their identity, and again to prove their address.

You can make it easier for the user to reuse a file by showing it as an option for the user to select instead of the file upload. Consider users on public devices before choosing to make the file available to preview or download. This is to comply with WCAG 2.2 success criterion 3.3.7 Redundant entry.

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="fileUpload1" 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.
disabled boolean If true, file input will be disabled.
describedBy string One or more element IDs to add to the aria-describedby attribute, used to provide additional descriptive information for screenreader users.
label object Required. The label used by the file upload component. See label.
hint object Can be used to add a hint to the file upload component. See hint.
errorMessage object Can be used to add an error message to the file upload component. The error message component will not display if you use a falsy value for errorMessage, for example false or null. See errorMessage.
formGroup object Additional options for the form group containing the file upload component. 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 object
Name Type Description
classes string Classes to add to the form group (for example to show error state for the whole group).
attributes object HTML attributes (for example data attributes) to add to the form group.
beforeInput object Content to add before the input used by the file upload component. See formGroup beforeInput.
afterInput object Content to add after the input used by the file upload component. See formGroup afterInput.
Options for formGroup beforeInput object
Name Type Description
text string Required. Text to add before the input. If html is provided, the text option will be ignored.
html string Required. HTML to add before the input. If html is provided, the text option will be ignored.
Options for formGroup afterInput object
Name Type Description
text string Required. Text to add after the input. If html is provided, the text option will be ignored.
html string Required. HTML to add after the input. If html is provided, the text option will be ignored.
Options for label component
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 option 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 option will be ignored.
for string 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 component
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 option 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 option will be ignored.
id string 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 "govuk/components/file-upload/macro.njk" import govukFileUpload %}

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

Error messages

Error messages should be styled like this:

<div class="govuk-form-group govuk-form-group--error">
  <label class="govuk-label" for="file-upload-1">
    Upload a file
  </label>
  <p id="file-upload-1-error" class="govuk-error-message">
    <span class="govuk-visually-hidden">Error:</span> The CSV must be smaller than 2MB
  </p>
  <input class="govuk-file-upload govuk-file-upload--error" id="file-upload-1" name="fileUpload1" type="file" aria-describedby="file-upload-1-error">
</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.
disabled boolean If true, file input will be disabled.
describedBy string One or more element IDs to add to the aria-describedby attribute, used to provide additional descriptive information for screenreader users.
label object Required. The label used by the file upload component. See label.
hint object Can be used to add a hint to the file upload component. See hint.
errorMessage object Can be used to add an error message to the file upload component. The error message component will not display if you use a falsy value for errorMessage, for example false or null. See errorMessage.
formGroup object Additional options for the form group containing the file upload component. 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 object
Name Type Description
classes string Classes to add to the form group (for example to show error state for the whole group).
attributes object HTML attributes (for example data attributes) to add to the form group.
beforeInput object Content to add before the input used by the file upload component. See formGroup beforeInput.
afterInput object Content to add after the input used by the file upload component. See formGroup afterInput.
Options for formGroup beforeInput object
Name Type Description
text string Required. Text to add before the input. If html is provided, the text option will be ignored.
html string Required. HTML to add before the input. If html is provided, the text option will be ignored.
Options for formGroup afterInput object
Name Type Description
text string Required. Text to add after the input. If html is provided, the text option will be ignored.
html string Required. HTML to add after the input. If html is provided, the text option will be ignored.
Options for label component
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 option 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 option will be ignored.
for string 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 component
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 option 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 option will be ignored.
id string 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 "govuk/components/file-upload/macro.njk" import govukFileUpload %}

{{ govukFileUpload({
  id: "file-upload-1",
  name: "fileUpload1",
  label: {
    text: "Upload a file"
  },
  errorMessage: {
    text: "The CSV must be smaller than 2MB"
  }
}) }}

Make sure errors follow the guidance in error message and have specific error messages for specific error states.

If no file has been selected

Say ‘Select a [whatever they need to select]’.

For example, ‘Select a report’.

If the file is the wrong file type

Say ‘The selected file must be a [list of file types]’.

For example, ‘The selected file must be a CSV or ODS’ or ‘The selected file must be a JPG, BMP, PNG, TIF or PDF’.

If the file is too big

Say ‘The selected file must be smaller than [largest file size]’.

For example, ‘The selected file must be smaller than 2MB’.

If the file is empty

Say ‘The selected file is empty’.

If the file contains a virus

Say ‘The selected file contains a virus’.

If the file is password protected

Say ‘The selected file is password protected’.

If there was a problem and the file was not uploaded

Say ‘The selected file could not be uploaded – try again’.

If there is a limit on how many files the user can select

Say ‘You can only select up to [highest number] files at the same time’.

For example, ‘You can only select up to 10 files at the same time’.

If the file is not in a template that must be used or the template has been changed

Say ‘The selected file must use the template’.

Research on this component

An accessibility audit has shown some users have encountered a problem when using file upload.

Known issues and gaps

The file upload component does not show a visual target area when dragging and dropping a file. We do not plan on fixing this at the moment, as the component inherits and uses the browser’s default behaviour. We will revisit this issue if we publish a custom file upload component. More detail on the findings can be found in the GitHub issue: ‘Upload file component has no visual target area when dragging and dropping a file’.

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

Help improve this component

To help make sure that this page is useful, relevant and up to date, you can:

Tell us if your service uses this component

Take part in our usage survey (opens in a new tab) to help us improve this component to better meet the needs of the services that use it.

Need help?

If you’ve got a question about the GOV.UK Design System, contact the team.