---
title: FileUpload
---

import LibraryInfo from '@theme/LibraryInfo'
import { Feedback, FigmaEmbed, PropsTable } from '../../src/components'
import { FileUpload } from '@telus-uds/components-base'
import { FileUpload as FileUploadDocs } from 'docs/@telus-uds/components-base'
import AvailableOnBoth from '../../partials/_platform-considerations-both.mdx'
import NoStylisticVariants from '../../partials/_variants-none.mdx'

# FileUpload

<LibraryInfo platform="multi-platform" packageName="components-base" figmaLink="" />

## Introduction

```jsx
import { FileUpload } from '@telus-uds/components-base'
```

A form element used to upload files.

[Follow the appropriate instructions](importing-components) to add this component in to your app.

```jsx live
function FileUploadExample() {
  const onUpload = async (files) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(files)
      }, 500)
    })
  }

  const onDelete = async (file) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(file)
      }, 500)
    })
  }

  return (
    <FileUpload
      copy={'en'}
      fileTypes={['pdf', 'png']}
      allowMultipleFiles={true}
      maxFileSize={5}
      maxFilesCount={3}
      minFilesCount={2}
      minFileSize={1}
      onUpload={onUpload}
      onDelete={onDelete}
    />
  )
}
```

## Guidance

- Use `FileUpload` to upload a document
- Use `Hint` to indicate file formats and file size requirements
- Provide detailed error state copy to help users recognize, diagnose, and recover from errors

### Accessibility

FileUpload has no semantic role or aria attributes applied by default.

### Uploading Files

To use the `FileUpload` component effectively, you need to provide `onUpload` and `onDelete` functions. These functions handle the file upload and deletion processes, respectively. Additionally, to manage error messages, you should include an `.error` property in the objects returned by these functions. This property should contain the error message you want to display.

#### Web

When the component is used on the web, the `onUpload` function receives a `FileList` object. Each file in the `FileList` is a `File` object from the `Web API`. The File object is a specific kind of Blob object, which means it inherits all the properties and methods of a `Blob`, but also includes additional properties related to files. The File object has the following properties:

- `name`: The name of the file. For example, `sample.pdf`.
- `size`: The size of the file in bytes. For example, `18810`.
- `type`: The MIME type of the file. For example, `application/pdf`.
- `lastModified`: The last modified timestamp of the file. For example, `1609459200000`.
- `lastModifiedDate`: The last modified date of the file as a Date object. For example, `new Date("2021-01-01T00:00:00.000Z")`.

Since the File object is a type of Blob, it can be used in any context where a Blob is expected, such as reading the file's data.

#### Mobile

When the component is used on mobile devices, the `onUpload` function receives an array of objects. Each object in the array has the following structure:

```json
[
  {
    "mimeType": "application/pdf",
    "name": "sample.pdf",
    "size": 18810,
    "type": "success",
    "uri": "file:///DocumentPicker/b1cf0fcd-a519-4eb1-ad65-9b9c727e271a.pdf"
  }
]
```

- `mimeType`: The MIME type of the file. For example, `application/pdf`. This indicates the type of file, such as PDF, image, text, etc.
- `name`: The name of the file. For example, `sample.pdf`. This is the filename as it appears on the device.
- `size`: The size of the file in bytes. For example, `18810`. This indicates how large the file is.
- `type`: The status of the file selection. For example, `success`. This can be used to indicate whether the file selection was successful or if there was an error.
- `uri`: The URI of the file. For example, `file:///DocumentPicker/b1cf0fcd-a519-4eb1-ad65-9b9c727e271a.pdf`. This is the location of the file on the device, which can be used to access the file.

These attributes provide all the necessary information about the file being uploaded from a mobile device.

### Error Handling

To handle error messages in the `FileUpload` component, you need to provide an `.error` property in the objects returned by the `onUpload` and `onDelete` functions. This property should contain the error message you want to display.

```jsx live
function FileUploadExample() {
  const onUpload = async (files) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const results = files.map((file, index) =>
          index % 2 === 0 ? { ...file, error: 'an api error' } : file
        )
        resolve(results)
      }, 500)
    })
  }

  const onDelete = async (file) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        const results = { ...file, error: 'an api error' }
        resolve(results)
      }, 500)
    })
  }

  return (
    <FileUpload
      copy={'en'}
      fileTypes={['pdf', 'png']}
      allowMultipleFiles={true}
      maxFileSize={1}
      maxFilesCount={2}
      onUpload={onUpload}
      onDelete={onDelete}
    />
  )
}
```

### Overriding the default dictionary

You can override the default dictionary by providing a custom dictionary object to the `dictionary` prop.

```jsx live
function FileUploadExample() {
  const onUpload = async (files) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(files)
      }, 500)
    })
  }

  const onDelete = async (file) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(file)
      }, 500)
    })
  }

  const customDictionary = {
    en: {
      label: 'Upload docs',
      buttonLabel: 'Choose docs',
      dismissButtonLabel: 'Remove file',
      wrongFileType: 'The selected file must be a %{fileType}.',
      allowedFileTypes: 'Allowed file types: %{fileTypes}.',
      fileTooBig: 'The selected file must be smaller than %{size}MB.',
      fileIsEmpty: 'The selected file is empty.',
      problemUploading: 'The selected file could not be uploaded. Try again.',
      problemDeleting: 'The selected file could not be deleted. Try again.',
      problemUploadingMultipleFiles: 'The selected docs could not be uploaded. Try again.',
      only: 'only',
      and: 'and',
      or: 'or',
      uploadSuccess: 'uploaded successfully.',
      uploadError: 'was not uploaded due to',
      deleteProblem: 'was not deleted due to',
      tooManyFiles: 'You can only select up to %{maxFilesCount} docs at the same time.',
      fileTooSmall: 'The selected file must be at least %{size}MB.',
      fewFiles: 'You must select at least %{minFilesCount} docs.'
    }
  }

  return (
    <FileUpload
      copy={'en'}
      fileTypes={['pdf', 'png']}
      allowMultipleFiles={true}
      maxFileSize={5}
      maxFilesCount={3}
      minFilesCount={2}
      minFileSize={1}
      onUpload={onUpload}
      onDelete={onDelete}
      dictionary={customDictionary}
    />
  )
}
```

### Using the component on React Native

To use the `FileUpload` component on mobile devices, you need to provide the `DocumentPicker` object from the `expo-document-picker` library. This library allows you to access the native document picker on both `iOS` and `Android`, enabling users to select files from their device.

:::info
Expo DocumentPicker must be installed and imported
`import * as DocumentPicker from 'expo-document-picker'`
:::

```JavaScript
import * as DocumentPicker from 'expo-document-picker'

function FileUploadExample() {
  const onUpload = async (files) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(files)
      }, 500)
    })
  }

  const onDelete = async (file) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(file)
      }, 500)
    })
  }

  return (
    <FileUpload
      copy={'en'}
      fileTypes={['pdf', 'png']}
      allowMultipleFiles={true}
      maxFileSize={5}
      maxFilesCount={2}
      onUpload={onUpload}
      onDelete={onDelete}
      documentPicker={DocumentPicker}
    />
  )
}
```

### Platform considerations

<AvailableOnBoth />

## Props

<PropsTable base={FileUploadDocs} />

## Variants

<NoStylisticVariants />

## Feedback

<Feedback component="FileUpload" />
