Upload
Upload file by selecting or dragging.
Install
npm install @arco-design/web-react@2.66.18
Import @arco-design/web-react/dist/css/arco.css at the entry and set the locale with ConfigProvider. React 19 projects must also import @arco-design/web-react/es/_util/react-19-adapter first, or imperative APIs such as Message and Modal cannot render.
Examples
- Basic: The Basic usage.
- Avatar: Click to upload user's avatar, and validate size and format of picture with beforeUpload.
- upload list: Default upload list.
- pictures wall:
onPreviewwill be executed when user click preview icon. The built-in image preview can be enabled via theimagePreviewproperty. (TheimagePreviewproperty is supported in2.41.0) - drag and drop: Drag files to a specific area, to upload.
- pictures with list: Pictures with list style.
- upload manually: If
autoUploadequalsfalse, files will not be uploaded automatically after being selected. You can submit them byuploadRef.submitmethod.submitwill only upload files that are infileList. - beforeUpload: The function will be executed before each file upload. Uploading will be aborted when the return value is false or a Promise which resolve(false) or reject.
- beforeUpload: The function will be executed before each file upload. Uploading will be aborted when the return value is false or a Promise which resolve(false) or reject.
- onRemove: The function will be executed when user click remove icon. Remove actions will be aborted when the return value is false or a Promise which resolve(false) or reject
- Limit: Limit the maximum number of uploaded files.
- Customize children: Customize the node that triggers the upload operation.
- custom icon:
showUploadListcan be an object to customizepreviewIcon,removeIcon,fileIcon,reuploadIcon,cancelIcon,startIcon,errorIconandfileName. - Customize upload list: Customize the display of uploaded files
- Custom request: Override for the default xhr behavior allowing for additional customization and ability to implement your own XMLHttpRequest
- customize progress bar: Use
progressPropsfor customize progress bar. - Upload directory: Upload directory.
Examples come from the official Arco Design repository (MIT) at the commit published as npm 2.66.18. Some use online images or upload endpoints; network requests are disabled in previews.
API
Upload
| Property | Description | Type | DefaultValue | Version |
|---|---|---|---|---|
| autoUpload | Whether to automatically upload files after selecting them | boolean | true | - |
| directory | Support upload whole directory | boolean | - | 2.11.0 |
| disabled | Whether to disable | boolean | - | - |
| drag | Whether to enable drag and drop upload | boolean | - | - |
| imagePreview | Enable built-in image preview, only works when listType='picture-card'. (v2.41.0) | boolean | - | - |
| multiple | Whether to allow multiple files to be selected | boolean | - | - |
| withCredentials | Whether to carry cookies when uploading requests | boolean | - | - |
| action | Uploading URL | string | - | - |
| method | The http method of upload request | string | post | 2.55.0 |
| listType | Upload list Style | 'text' | 'picture-list' | 'picture-card' | text | - |
| tip | The tip text | string | React.ReactNode | - | - |
| accept | Accepted file types(strict in 2.53.0, defaultValue is true. When set to false, accept behaves the same as native. When set to true, file extensions will be strictly matched and files that do not meet the accept rules will be filtered out. ) | string | { type: string; strict?: boolean } | - | - |
| beforeUpload | Callback before uploading. Uploading will be aborted when the return value is false or a Promise which resolve(false) or reject. | (file: File, filesList: File[]) => boolean | Promise<any> | () => true | - |
| className | Additional css class | string | string[] | - | - |
| defaultFileList | Default list of files that have been uploaded | UploadItem[] | - | - |
| fileList | List of files that have been uploaded | UploadItem[] | - | - |
| headers | Set request headers | object | - | - |
| limit | maximum number of uploads allowed. Object type is supported in 2.28.0 | number | { maxCount: number; hideOnExceedLimit?: boolean } | - | - |
| onRemove | Callback when the remove icon is clicked.Remove actions will be aborted when the return value is false or a Promise which resolve(false) or reject. | (file: UploadItem, fileList: UploadItem[]) => void | boolean | Promise<void | boolean> | - | - |
| progressProps | The props of Progress component. | Partial<ProgressProps> | - | - |
| showUploadList | Whether to show upload list.It can be an object to customize the previewIcon, removeIcon,fileIcon, reuploadIcon, cancelIcon, startIcon, errorIcon and fileName | boolean | CustomIconType | true | - |
| style | Additional style | CSSProperties | - | - |
| customRequest | Provide an override for the default xhr behavior for additional customization | (options: RequestOptions) => UploadRequestReturn | void | - | - |
| data | additions params of request | object | ((any: any) => object) | - | - |
| name | The key name of uploading file | string | ((any: any) => string) | - | - |
| onChange | Callback when uploading state is changing | (fileList: UploadItem[], file: UploadItem) => void | - | - |
| onDragLeave | Callback when drag and drop uploaded file leaves the drag area | (e: React.DragEvent) => void | - | 2.41.0 |
| onDragOver | Callback when drag and drop uploaded file into the drag area | (e: React.DragEvent) => void | - | 2.41.0 |
| onDrop | Callback after drag file to the upload area and drop. | (e: React.DragEvent) => void | - | 2.37.0 |
| onExceedLimit | Callback when limit is exceeded | (files: File[], fileList: UploadItem[]) => void | - | - |
| onPreview | Callback when the preview icon is clicked | (file: UploadItem) => void | - | - |
| onProgress | Callback when uploading progress is changing | (file: UploadItem, e?: ProgressEvent) => void | - | - |
| onReupload | Callback when the re-upload icon is clicked | (file: UploadItem) => void | - | - |
| renderUploadItem | Custom item of uploadList | (originNode: ReactNode, file: UploadItem, fileList: UploadItem[]) => ReactNode | - | - |
| renderUploadList | Custom uploadList | (fileList: UploadItem[], uploadListProps: UploadListProps) => ReactNode | - | - |
UploadListProps
File upload list display
| Property | Description | Type | DefaultValue |
|---|---|---|---|
| disabled | Whether to disable | boolean | - |
| onRemove | Callback when the remove icon is clicked.Remove actions will be aborted when the return value is false or a Promise which resolve(false) or reject | (file: UploadItem) => void | boolean | Promise<void | boolean> | - |
| onAbort | Callback when the cancel icon is clicked | (file: UploadItem) => void | - |
| onPreview | Callback when the preview icon is clicked | (file: UploadItem) => void | - |
| onReupload | Callback when the re-upload icon is clicked | (file: UploadItem) => void | - |
UploadItem
| Property | Description | Type | DefaultValue |
|---|---|---|---|
| percent | Upload progress percentage | number | - |
| name | File name | string | - |
| uid | Unique identifier | string (Required) | - |
| url | File url | string | - |
| originFile | File Object | File | - |
| response | Response of upload request | object | - |
| status | Uploading status | UploadStatus | - |
RequestOptions
export type RequestOptions = Pick<
UploadProps,
"headers" | "name" | "data" | "withCredentials" | "action" | "method"
> & {
/** 更新当前文件的上传进度 。percent: 当前上传进度百分比 */
onProgress: (percent: number, event?: ProgressEvent) => void;
/** 上传成功后,调用onSuccess方法,传入的response参数将会附加到当前上传文件的reponse字段上 */
onSuccess: (response?: object) => void;
/** 上传失败后,调用onError方法,传入的 response 参数将会附加到当前上传文件的response字段 */
onError: (response?: object) => void;
/** 当前上传文件 */
file: File;
};
UploadRequestReturn
export interface UploadRequestReturn {
abort?: () => void;
[key: string]: any;
}
CustomIconType
export type CustomIconType = {
previewIcon?: ReactNode;
removeIcon?: ReactNode;
fileIcon?: ReactNode;
reuploadIcon?: ReactNode;
cancelIcon?: ReactNode;
startIcon?: ReactNode;
errorIcon?: ReactNode;
successIcon?: ReactNode;
fileName?: (file: UploadItem) => ReactNode;
// 2.34.0
progressRender?: (
file: UploadItem,
originDom: ReactNode
) => React.ReactElement;
// 2.34.0
imageRender?: (file: UploadItem) => React.ReactNode;
};
UploadStatus
export type UploadStatus = "init" | "uploading" | "done" | "error";
Methods
| Property | Description | Type |
|---|---|---|
| submit | upload the file list manually. example. If no parameters, all files in the init state will be uploaded by default. | (file?: UploadItem) => void |
| abort | cancel upload request | (file: UploadItem) => void |
| reupload | re-upload request | (file: UploadItem) => void |
// Custom icons
type CustomIconType = {
previewIcon?: ReactNode;
removeIcon?: ReactNode;
fileIcon?: ReactNode;
reuploadIcon?: ReactNode;
cancelIcon?: ReactNode;
startIcon?: ReactNode;
errorIcon?: ReactNode;
fileName?: (file: UploadItem) => ReactNode;
progressRender?: (file: UploadItem, originDom: ReactNode) => ReactElement; // 2.34.0
imageRender?: (file: UploadItem) => ReactNode; // 2.34.0
}
Q&A
How to control the status and progress of uploaded files?
See example,
You can directly set the status, percent and other fields for each element in the fileList.