Skip to content

Upload

Rules

Basic Example

js
const rule = {
    type: "upload",
    field: "pic",
    title: "Carousel Images",
    value: [
        'http://img1.touxiang.cn/uploads/20131030/30-075657_191.jpg',
        'http://img1.touxiang.cn/uploads/20131030/30-075657_191.jpg'
        ],
    props: {
        action: "/upload.php",
        limit: 2,
        onSuccess: function (file) {
            file.url = file.response.url;
        }
    },
}

Props Configuration Examples

Image Upload

js
const rule = {
    type: "upload",
    field: "image",
    title: "Product Images",
    value: [],
    props: {
        action: "/upload.php",
        listType: "picture-card",
        accept: "image/*",
        limit: 5,
        multiple: true,
        onSuccess: function (file) {
            file.url = file.response.url;
        }
    },
}

File Upload

js
const rule = {
    type: "upload",
    field: "file",
    title: "Attachment Upload",
    value: [],
    props: {
        action: "/upload.php",
        listType: "text",
        accept: ".pdf,.doc,.docx",
        limit: 3,
        multiple: true,
        onSuccess: function (file) {
            file.url = file.response.url;
        }
    },
}

Limit File Size

js
const rule = {
    type: "upload",
    field: "file",
    title: "File Upload",
    value: [],
    props: {
        action: "/upload.php",
        accept: "image/*",
        onBeforeUpload: function (file) {
            if (file.size > 2 * 1024 * 1024) {
                return false; // Limit to 2MB
            }
            return true;
        },
        onSuccess: function (file) {
            file.url = file.response.url;
        }
    },
}

Drag and Drop Upload

js
const rule = {
    type: "upload",
    field: "file",
    title: "Drag and Drop Upload",
    value: [],
    props: {
        action: "/upload.php",
        draggable: true,
        onSuccess: function (file) {
            file.url = file.response.url;
        }
    },
}

Events Examples

Handle Upload Status

js
const rule = {
    type: "upload",
    field: "file",
    title: "File Upload",
    value: [],
    props: {
        action: "/upload.php",
        onSuccess: function (file) {
            file.url = file.response.url;
        },
    },
    on: {
        change: (fileList, fileItem) => {
            console.log('File status changed:', fileItem.status);
        },
        error: (fileItem) => {
            console.log('Upload failed:', fileItem);
        },
        progress: (fileItem) => {
            console.log('Upload progress:', fileItem.percent);
        },
    },
}

Update Other Fields After Upload

js
const rule = [
    {
        type: "upload",
        field: "cover",
        title: "Cover Image",
        value: [],
        props: {
            action: "/upload.php",
            listType: "picture-card",
            accept: "image/*",
            limit: 1,
            onSuccess: function (file) {
                file.url = file.response.url;
            },
        },
        inject: true,
        on: {
            change: ($inject, fileList, fileItem) => {
                // Fill image URL after successful upload
                if (fileItem.status === 'done' && fileItem.url) {
                    $inject.api.setValue('coverUrl', fileItem.url);
                }
            },
        },
    },
    {
        type: "input",
        field: "coverUrl",
        title: "Cover URL",
        props: {
            disabled: true,
        },
    },
]

Complete configuration items: arco-design_Upload

value: Array | String

Note

After a successful upload, assign the URL from the API response to file.url via the onSuccess callback. Otherwise, the form cannot retrieve the component's data.

Props

ParameterDescriptionTypeDefaultVersion
onSuccessCallback when upload succeeds(fileItem: FileItem) => void-
acceptAccepted upload file types, refer to HTML Standardstring-
actionUpload URLstring-
disabledWhether disabledbooleanfalse
multipleWhether to support multiple file uploadbooleanfalse
directoryWhether to support folder upload (requires browser support)booleanfalse
draggableWhether to support drag and drop uploadbooleanfalse
tipTip textstring-
headersAdditional header information for upload requestobject-
dataAdditional data for upload requestRecord<string, unknown>| ((fileItem: FileItem) => Record<string, unknown>)-
nameUploaded file namestring | ((fileItem: FileItem) => string)-
with-credentialsWhether upload request carries cookiesbooleanfalse
custom-requestCustom upload behavior(option: RequestOption) => UploadRequest-
limitLimit the number of uploaded files. 0 means unlimitednumber0
auto-uploadWhether to automatically upload filesbooleantrue
show-file-listWhether to show file listbooleantrue
show-remove-buttonWhether to show remove buttonbooleantrue2.11.0
show-retry-buttonWhether to show retry buttonbooleantrue2.11.0
show-cancel-buttonWhether to show cancel buttonbooleantrue2.11.0
show-upload-buttonWhether to show upload button. Version 2.14.0 added showOnExceedLimit supportboolean | { showOnExceedLimit: boolean }true2.11.0
downloadWhether to add download attribute to <a> linkbooleanfalse2.11.0
show-linkIn list mode, if uploaded file has URL, show link. If disabled, only show text and clicking can trigger preview event.booleantrue2.13.0
image-loadingNative HTML attribute for <img>, requires browser support'eager' | 'lazy'-2.11.0
list-typeImage list type'text' | 'picture' | 'picture-card''text'
response-url-keyKey to get image URL from Response, when enabled, uploaded images will replace preloaded imagesstring | ((fileItem: FileItem) => string)-
custom-iconCustom iconCustomIcon-
image-previewWhether to use ImagePreview component for previewbooleanfalse2.14.0
on-before-uploadTriggered before uploading image(file: File) => Promise<boolean>-
on-before-removeTriggered before removing image(fileItem: FileItem) => Promise<boolean>-
on-button-clickTriggered when upload button is clicked (if returns Promise, default input upload will be disabled)(event: Event) => Promise<FileList> | void-

Events

Event NameDescriptionParameters
exceed-limitTriggered when uploaded images exceed limitfileList: FileItem[] files: File[]
changeTriggered when uploaded image status changesfileList: FileItem[] fileItem: fileItem
progressTriggered when upload progress changesfileItem: fileItem event: ProgressEvent
previewTriggered when clicking image previewfileItem: FileItem
errorTriggered when upload failsfileItem: FileItem

FormCreate is an open-source project released under the MIT License. Free for personal and commercial use.