FileUpload is an advanced uploader with dragdrop support, multi file uploads, auto uploading, progress tracking and validations.
Drag and drop files to here to upload.
import { FileUpload } from 'primereact/fileupload';
<script src="https://unpkg.com/primereact/core/core.min.js"></script>
<script src="https://unpkg.com/primereact/fileupload/fileupload.min.js"></script>
FileUpload requires a url property as the upload target and a name to identify the files at backend.
<FileUpload name="demo" url="./upload"></FileUpload>
Only one file can be selected at a time by default, to allow selecting multiple files at once enable multiple option.
<FileUpload name="demo[]" url="./upload" multiple />
File selection can also be done by dragging and dropping from the filesystem to the content section of the component.
When auto property is enabled, upload begins as soon as file selection is completed or a file is dropped on the drop area.
<FileUpload name="demo" url="./upload" auto />
Selectable file types can be restricted with accept property, example below only allows images to be uploaded. Read more about other possible values here.
<FileUpload name="demo[]" url="./upload" multiple accept="image/*" />
Maximium file size can be restricted using maxFileSize property defined in bytes.
<FileUpload name="demo" url="./upload" maxFileSize="1000000" />
In order to customize the default messages use invalidFileSizeMessageSummary and invalidFileSizeMessageDetail options. In summary messages, 0 placeholder refers to the filename and in detail message, the file size.
XHR request to upload the files can be customized using the onBeforeUpload callback that passes the xhr instance and FormData object as event parameters.
FileUpload basic mode provides a simpler UI as an alternative to advanced mode.
<FileUpload name="demo" url="./upload" mode="basic" />
Uploading implementation can be overriden by enabling customUpload property and defining a custom upload handler event.
<FileUpload name="demo[]" url="./upload" customUpload uploadHandler={myUploader} />
const myUploader = (event) => {
//event.files == files to upload
}
Used to create custom item elements in the container.
<FileUpload name="demo[]" url="./upload" itemTemplate={customItemTemplate} uploadHandler={myUploader} />
const customItemTemplate = (file, props) => {
// file: Current file object.
// options.onRemove: Event used to remove current file in the container.
// options.previewElement: The default preview element in the container.
// options.fileNameElement: The default fileName element in the container.
// options.sizeElement: The default size element in the container.
// options.removeElement: The default remove element in the container.
// options.formatSize: The formated size of file.
// options.files: Current files.
// options.index: The index of file in current files list.
// options.element: Default element created by the component.
// options.props: component props.
}
Used to customize choose, upload and cancel buttons.
const chooseOptions = {label: 'Choose', icon: 'pi pi-fw pi-plus'};
const uploadOptions = {label: 'Uplaod', icon: 'pi pi-upload', className: 'p-button-success'};
const cancelOptions = {label: 'Cancel', icon: 'pi pi-times', className: 'p-button-danger'};
<FileUpload name="demo[]" url="./upload" chooseOptions={chooseOptions} uploadOptions={uploadOptions} cancelOptions={cancelOptions} uploadHandler={myUploader} />
const buttonOptions = {
// label: The label of button.
// icon: The icon of button.
// className: Style class of button.
// style: Style of button.
}
Name | Type | Default | Description |
---|---|---|---|
id | string | null | Unique identifier of the element. |
name | string | null | Name of the request parameter to identify the files at backend. |
url | string | null | Remote url to upload the files. |
mode | string | advanced | Defines the UI of the component, possible values are "advanced" and "basic". |
multiple | boolean | false | Used to select multiple files at once from file dialog. |
accept | string | false | Pattern to restrict the allowed file types such as "image/*". |
disabled | boolean | false | Disables the upload functionality. |
auto | boolean | false | When enabled, upload begins automatically after selection is completed. |
maxFileSize | number | null | Maximum file size allowed in bytes. |
invalidFileSizeMessageSummary | string | "{0}: Invalid file size, " | Summary message of the invalid fize size. |
invalidFileSizeMessageDetail | string | "maximum upload size is {0}." | Detail message of the invalid fize size. |
style | object | null | Inline style of the component. |
className | string | null | Style class of the component. |
withCredentials | boolean | false | Cross-site Access-Control requests should be made using credentials such as cookies, authorization headers or TLS client certificates. |
previewWidth | number | 50 | Width of the image thumbnail in pixels. |
chooseLabel | string | null | Label of the choose button. Defaults to global value in Locale configuration. |
uploadLabel | string | null | Label of the upload button. Defaults to global value in Locale configuration. |
cancelLabel | string | null | Label of the cancel button. Defaults to global value in Locale configuration. |
chooseOptions | object (OptionsType) | null | Options used to customize the choose button. These options have "label", "icon", "className" and "style" properties. |
uploadOptions | object (OptionsType) | null | Options used to customize the upload button. These options have "label", "icon", "className" and "style" properties. |
cancelOptions | object (OptionsType) | null | Options used to customize the cancel button. These options have "label", "icon", "className" and "style" properties. |
customUpload | boolean | false | Whether to use the default upload or a manual implementation defined in uploadHandler callback. |
emptyTemplate | any | null | The template of empty content in the container. |
progressBarTemplate | any | null | The template of progressBar content in the container. |
itemTemplate | any | null | The template of each item content in the container. |
headerTemplate | any | null | The template of the header. |
headerStyle | object | null | Inline style of the header. |
headerClassName | string | null | Style class of the header. |
contentStyle | object | null | Inline style of the content. |
contentClassName | string | null | Style class of the content. |
Name | Parameters | Description |
---|---|---|
onBeforeUpload | event.xhr: XmlHttpRequest instance. event.formData: FormData object. | Callback to invoke before file upload begins to customize the request such as post parameters before the files. |
onBeforeSend | event.xhr: XmlHttpRequest instance. event.formData: FormData object. | Callback to invoke before file send begins to customize the request such as adding headers. |
onBeforeDrop | event: DragEvent instance. | Callback to invoke before files dropped. Return false from callback to prevent drop. |
onBeforeSelect | event.originalEvent: Original browser event. event.target.files: List of selected files. | Callback to invoke before files are selected. Return false from callback to prevent selection. |
onUpload | event.xhr: XmlHttpRequest instance. event.files: Uploaded files. | Callback to invoke when file upload is complete. |
onError | event.xhr: XmlHttpRequest instance. event.files: Files that are not uploaded. | Callback to invoke if file upload fails. |
onClear | - | Callback to invoke when files in queue are removed without uploading. |
onSelect | event.originalEvent: Original browser event. event.target.files: List of selected files. | Callback to invoke when files are selected. |
onProgress | event.originalEvent: Original browser event. event.progress: Calculated progress value. | Callback to invoke when files are being uploaded. |
onValidationFail | file: Invalid file. | Callback to invoke when a validation file fails. |
uploadHandler | event.files: List of selected files. event.options: Handler options. | Callback to invoke in custom upload mode to upload the files manually. |
onRemove | event.originalEvent: Original browser event. event.file: Selected file. | Callback to invoke when a file is removed without uploading using clear button of a file. |
Name | Parameters | Description |
---|---|---|
upload | - | Uploads the selected files. |
clear | - | Clears the files list. |
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-fileupload | Container element. |
p-fileupload-buttonbar | Header containing the buttons. |
p-fileupload-content | Content section. |
None.
Built-in component themes created by the PrimeReact Theme Designer.
Premium themes are only available exclusively for PrimeReact Theme Designer subscribers and therefore not included in PrimeReact core.
Beautifully crafted premium create-react-app application templates by the PrimeTek design team.