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",
        max: 2,
        onSuccess:function (res, file) {
            file.url = res.url;
        }
    },
}

Props Configuration Examples

Image Upload

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

File Upload

js
const rule = {
    type: "upload",
    field: "file",
    title: "Attachment Upload",
    value: [],
    props: {
        action: "/upload.php",
        listType: "text",
        accept: ".pdf,.doc,.docx",
        max: 3,
        multiple: true,
        onSuccess: function (res, file) {
            file.url = res.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.file.size > 2 * 1024 * 1024) {
                return false; // Limit 2MB
            }
            return true;
        },
        onSuccess: function (res, file) {
            file.url = res.url;
        }
    },
}

Custom Upload

js
const rule = {
    type: "upload",
    field: "file",
    title: "Custom Upload",
    value: [],
    props: {
        customRequest: function ({ file, onFinish, onError }) {
            // Custom upload logic
            const formData = new FormData();
            formData.append('file', file.file);


fetch('/upload.php', {
                method: 'POST',
                body: formData
            }).then(res => res.json()).then(data => {
                file.url = data.url;
                onFinish();
            }).catch(onError);
        },
        onSuccess: function (res, file) {
            file.url = res.url;
        }
    },
}

Events Examples

Handle Upload Status

js
const rule = {
    type: "upload",
    field: "file",
    title: "File Upload",
    value: [],
    props: {
        action: "/upload.php",
        onSuccess: function (res, file) {
            file.url = res.url;
        },
        onChange: function ({ file, fileList }) {
            console.log('File status changed:', file.status);
        },
        onError: function ({ file }) {
            console.log('Upload failed:', file);
        },
        onFinish: function ({ file }) {
            console.log('Upload completed:', file);
        },
    },
}

Update Other Fields After Upload

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

Complete configuration items: naive-ui_Upload

value :Array | String

Note

After the file is successfully uploaded, you need to assign the url from the interface response to file.url through the onSuccess callback. Otherwise, the form cannot get the component's data.

Props

NameTypeDefault ValueDescriptionVersion
onSuccessFunctionundefinedCallback after file upload succeeds
abstractbooleanfalseWhether DOM wrapper exists, does not support image-card type Upload
acceptstringundefinedAccepted file types, refer to accept
actionstringundefinedRequest submission address
create-thumbnail-url(file: File) => Promise<string>undefinedCustom file thumbnail
custom-request(options: UploadCustomRequestOptions) => voidundefinedCustom upload method, type reference UploadCustomRequestOptions
dataObject | ({ file: UploadFileInfo }) => ObjectundefinedAdditional data needed for form submission
default-file-listArray<UploadFileInfo>[]Default file list in uncontrolled state
default-uploadbooleantrueWhether to upload by default when selecting files
disabledbooleanfalseWhether disabled
file-list-styleObjectundefinedStyle of file list area
file-listArray<UploadFileInfo>undefinedFile list, if passed in, component will be in controlled state
headersObject | ({ file: UploadFileInfo }) => ObjectundefinedAdditional Headers needed for HTTP request
image-group-propsImageGroupPropsundefinedProps of preview image component in Upload, refer to ImageGroup Props2.24.0
input-propsObjectundefinedProps of file input element2.24.2
list-typestring'text'Built-in style of file list, text, image and image-card
maxnumberundefinedLimit the number of uploaded files
methodstring'POST'HTTP request method
multiplebooleanfalseWhether to support multiple files
namestring'file'Field name of file in form submission
show-cancel-buttonbooleantrueWhether to show cancel button (displayed when pending, uploading, error), clicking cancel button will trigger on-remove callback
show-download-buttonbooleanfalseWhether to show download button (displayed after finished)
show-remove-buttonbooleantrueWhether to show delete button (displayed after finished), clicking delete button will trigger on-remove callback
show-retry-buttonbooleantrueWhether to show retry upload button (displayed when error)
show-file-listbooleantrueWhether to show file list
show-preview-buttonbooleantrueWhether to allow showing preview button (effective when list-type is image-card)
show-triggerbooleantrueWhether to show trigger element2.21.5
with-credentialsbooleanfalseWhether to carry Cookie
on-change(options: { file: UploadFileInfo, fileList: Array<UploadFileInfo>, event?: Event }) => void | () => {}Callback when component state changes, any file state change in component will trigger callback
on-error(options: { file: UploadFileInfo, event?: ProgressEvent }) => UploadFileInfo | void | undefinedCallback when file upload fails2.24.0
on-finish(options: { file: UploadFileInfo, event?: ProgressEvent }) => UploadFileInfo | void | ({ file }) => fileCallback when file upload ends, can modify the passed UploadFileInfo or return a new UploadFileInfo. Note: file will be set to null in the next event loop
on-before-upload(options: { file: UploadFileInfo, fileList: UploadFileInfo[] }) => (Promise<boolean | void> | boolean | void)undefinedCallback before file upload, returning false, Promise resolve false, Promise rejected will cancel this upload
on-download(file: FileInfo) => voidundefinedCallback function when clicking file download button
on-preview(file: FileInfo) => voidundefinedCallback function when clicking file link or preview button
on-remove(options: { file: UploadFileInfo, fileList: Array<UploadFileInfo> }) => Promise<boolean> | boolean | any | () => trueFile delete callback, returning false, Promise resolve false, Promise rejected will cancel this deletion
on-update:file-list(fileList: UploadFileInfo[]) => voidundefinedCallback function triggered when file-list changes

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