| 1 |
--- |
| 2 |
id: Simple file upload |
| 3 |
cssPrefix: pf-v6-c-file-upload |
| 4 |
propComponents: ['FileUpload', 'FileUploadField'] |
| 5 |
section: components |
| 6 |
subsection: file-upload |
| 7 |
--- |
| 8 |
|
| 9 |
import { useState } from 'react'; |
| 10 |
import FileUploadIcon from '@patternfly/react-icons/dist/esm/icons/file-upload-icon'; |
| 11 |
|
| 12 |
## Examples |
| 13 |
|
| 14 |
The `FileUpload` component can accept a file via browse or drag-and-drop, and behaves like a standard form field with its `value` and `onFileInputChange` event that is similar to `<input onChange="...">` prop. The `type` prop determines how the `FileUpload` component behaves upon accepting a file, what type of value it passes to its `onDataChange` event. |
| 15 |
|
| 16 |
### Text files |
| 17 |
|
| 18 |
If `type="text"` is passed (and `hideDefaultPreview` is not), a `TextArea` preview will be rendered underneath the filename bar. When a file is selected, its contents will be read into memory and passed to the `onDataChange` event as a string. Every filename change is passed to `onFileInputChange` same as it would do with the `<input>` element. |
| 19 |
Pressing _Clear_ button triggers `onClearClick` event. |
| 20 |
|
| 21 |
### Simple text file |
| 22 |
|
| 23 |
```ts file="./FileUploadSimpleText.tsx" |
| 24 |
``` |
| 25 |
|
| 26 |
### With helper text |
| 27 |
|
| 28 |
You can pass in the `<FileUploadHelperText` sub-component via the `children` property to `<FileUpload>`. |
| 29 |
|
| 30 |
```ts file="./FileUploadWithHelperText.tsx" |
| 31 |
``` |
| 32 |
|
| 33 |
### Text file with edits allowed |
| 34 |
|
| 35 |
A user can always type instead of selecting a file, but by default, once a user selects a text file from their disk they are not allowed to edit it (to prevent unintended changes to a format-sensitive file). This behavior can be changed with the `allowEditingUploadedText` prop. |
| 36 |
Typing/pasting text in the box will call `onTextChange` with a string, and a string value is expected for the `value` prop. : |
| 37 |
|
| 38 |
```ts file="./FileUploadTextWithEdits.tsx" |
| 39 |
``` |
| 40 |
|
| 41 |
### Restricting file size and type |
| 42 |
|
| 43 |
Any [props accepted by `react-dropzone`'s `Dropzone` component](https://react-dropzone.js.org/#!/Dropzone) can be passed as a `dropzoneProps` object in order to customize the behavior of the Dropzone, such as restricting the size and type of files allowed. You can also capture and act upon native `react-dropzone` errors using the exposed `DropzoneErrorCode` enum. The following example will only accept CSV files smaller than 1 KB. Note that file type determination is not reliable across platforms (see the note on react-dropzone's docs about the `accept` prop), so be sure to test the behavior of your file upload restriction on all browsers and operating systems targeted by your application. |
| 44 |
|
| 45 |
#### IMPORTANT: A note about security |
| 46 |
|
| 47 |
Restricting file sizes and types in this way is for user convenience only, and it cannot prevent a malicious user from submitting anything to your server. As with any user input, your application should also validate, sanitize and/or reject restricted files on the server side. |
| 48 |
|
| 49 |
### Text file with restrictions |
| 50 |
|
| 51 |
```ts file="./FileUploadTextWithRestrictions.tsx" |
| 52 |
``` |
| 53 |
|
| 54 |
### Other file types |
| 55 |
|
| 56 |
If no `type` prop is specified, the component will not read files directly. When a file is selected, a [`File` object](https://developer.mozilla.org/en-US/docs/Web/API/File) will be passed as a second argument to `onFileInputChange` and your application will be responsible for reading from it (e.g. by using the [FileReader API](https://developer.mozilla.org/en-US/docs/Web/API/FileReader) or attaching it to a [FormData object](https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects)). A `File` object will also be expected for the `value` prop instead of a string, and no preview of the file contents will be shown by default. The `onReadStarted` and `onReadFinished` callbacks will also not be called since the component is not reading the file. |
| 57 |
|
| 58 |
### Simple file of any format |
| 59 |
|
| 60 |
```ts file="./FileUploadSimpleFile.tsx" |
| 61 |
``` |
| 62 |
|
| 63 |
### Customizing the file preview |
| 64 |
|
| 65 |
Regardless of `type`, the preview area (the TextArea, or any future implementations of default previews for other types) can be removed by passing `hideDefaultPreview`, and a custom one can be rendered by passing `children`. |
| 66 |
|
| 67 |
### Custom file preview |
| 68 |
|
| 69 |
```ts file="./FileUploadCustomPreview.tsx" |
| 70 |
``` |
| 71 |
|
| 72 |
### Bringing your own file browse logic |
| 73 |
|
| 74 |
`FileUpload` is a thin wrapper around the `FileUploadField` presentational component. If you need to implement your own logic for accepting files, you can instead render a `FileUploadField` directly, which does not include `react-dropzone` and requires additional props (e.g. `onBrowseButtonClick`, `onClearButtonClick`, `isDragActive`). |
| 75 |
|
| 76 |
Note that the `isLoading` prop is styled to position the spinner dead center above the entire component, so it should not be used with `hideDefaultPreview` unless a custom empty-state preview is provided via `children`. The below example prevents `isLoading` and `hideDefaultPreview` from being used at the same time. You can always provide your own spinner as part of the `children`! |
| 77 |
|
| 78 |
### Custom file upload |
| 79 |
|
| 80 |
```ts file="./FileUploadCustomUpload.tsx" |
| 81 |
``` |
| 82 |
|
| 83 |
## Types |
| 84 |
|
| 85 |
File upload uses the `DropzoneOptions` type from react-dropzone. It is comprised of additional props with their own types. For more information on using `DropzoneOptions` visit [react-dropzone props and methods](https://react-dropzone.js.org/#src). |
| 86 |
|
| 87 |
Additionally, it calls the `onDataChange`, `onReadFailed`, `onReadFinished`, and `onReadStarted` callbacks with an event of type `DropEvent`. `DropEvent` is a union comprised of the following types: |
| 88 |
|
| 89 |
```noLive |
| 90 |
React.DragEvent<HTMLElement> |
| 91 |
| React.ChangeEvent<HTMLInputElement> |
| 92 |
| DragEvent |
| 93 |
| Event |
| 94 |
``` |