Skip to content

Uploader

uploader

Accept File Types

For valid file type values, see MDN documentation on accept attribute

Rule

Basic Example

js
const rule = {
    type: 'uploader',
    title: 'Upload',
    field: 'uploader',
    value: [],
    props: {
        action: 'https://run.mocky.io/v3/88bff269-1d6d-4799-8b3b-5736402b3d4a',
        onSuccess(res, file) {
            file.url = res.url;
        }
    }
}

Props Configuration Examples

Single Image Upload

js
const rule = {
    type: 'uploader',
    title: 'Avatar',
    field: 'avatar',
    value: [],
    props: {
        action: '/upload.php',
        maxCount: 1,
        accept: 'image/*',
        onSuccess(res, file) {
            file.url = res.url;
        }
    }
}

Multiple Image Upload

js
const rule = {
    type: 'uploader',
    title: 'Product Images',
    field: 'gallery',
    value: [],
    props: {
        action: '/upload.php',
        multiple: true,
        accept: 'image/*',
        maxCount: 9,
        onSuccess(res, file) {
            file.url = res.url;
        }
    }
}

File Upload

js
const rule = {
    type: 'uploader',
    title: 'Document Upload',
    field: 'document',
    value: [],
    props: {
        action: '/upload.php',
        accept: '.pdf,.doc,.docx',
        maxCount: 5,
        onSuccess(res, file) {
            file.url = res.url;
        }
    }
}

Validation Before Upload

js
const rule = {
    type: 'uploader',
    title: 'Avatar',
    field: 'avatar',
    value: [],
    props: {
        action: '/upload.php',
        maxCount: 1,
        accept: 'image/*',
        maxSize: 2 * 1024 * 1024, // 2MB
        beforeRead: (file) => {
            // Validate file size before upload
            if (file.size > 2 * 1024 * 1024) {
                alert('Image size cannot exceed 2MB');
                return false;
            }
            return true;
        },
        onSuccess(res, file) {
            file.url = res.url;
        }
    }
}

Events Examples

Listen to Upload Events

js
const rule = {
    type: 'uploader',
    title: 'Image Upload',
    field: 'pic',
    value: [],
    props: {
        action: '/upload.php',
        accept: 'image/*',
        onSuccess(res, file) {
            file.url = res.url;
        },
        afterRead: (file) => {
            console.log('File read completed:', file);
        },
        beforeRead: (file) => {
            console.log('Before file read:', file);
            return true;
        },
        beforeDelete: (file) => {
            console.log('Before delete:', file);
            return true;
        },
    },
    on: {
        oversize: (file) => {
            console.log('File size exceeds limit:', file);
        },
        'click-upload': (event) => {
            console.log('Upload area clicked:', event);
        },
        delete: (file) => {
            console.log('File deleted:', file);
        },
    },
}

Complete configuration items:Vant_Uploader

value :Array | String

Note

After a successful upload, assign the URL from the API response to file.url in the onSuccess callback. Otherwise, the form cannot access the component data.

Props

ParameterDescriptionTypeDefault Value
acceptAllowed file types for upload, Detailed descriptionstringimage/*
nameIdentifier, usually a unique string or number, can be obtained in the second parameter of the callback functionnumber | string-
preview-sizeSize of preview image and upload area, default unit is pxnumber | string | Array80px
preview-imageWhether to show preview image after upload completesbooleantrue
preview-full-imageWhether to show full-screen image preview after clicking preview imagebooleantrue
preview-optionsConfiguration items for full-screen image preview, see ImagePreview for optional valuesobject-
multipleWhether to enable multiple image selection, some Android devices do not supportbooleanfalse
disabledWhether to disable file uploadbooleanfalse
readonlyWhether to set upload area to readonly statebooleanfalse
deletableWhether to show delete buttonbooleantrue
reuploadWhether to enable overwrite upload, will close image preview when enabledbooleanfalse
show-uploadWhether to show upload areabooleantrue
lazy-loadWhether to enable image lazy loading, must be used with Lazyload componentbooleanfalse
captureImage selection mode, optional value is camera (directly open camera)string-
after-readCallback function after file read completesFunction-
before-readCallback function before file read, return false to terminate file read, supports returning PromiseFunction-
before-deleteCallback function before file delete, return false to terminate file read, supports returning PromiseFunction-
max-sizeFile size limit, unit is bytenumber | string | (file: File) => booleanInfinity
max-countFile upload count limitnumber | stringInfinity
result-typeFile read result type, optional values are file textstringdataUrl
upload-textUpload area text hintstring-
image-fitPreview image crop mode, see Image component for optional valuesstringcover
upload-iconUpload area icon name or image link, same as Icon component's name attributestringphotograph

Events

Event NameDescriptionCallback Parameters
oversizeTriggered when file size exceeds limitSame as after-read
click-uploadTriggered when upload area is clickedevent: MouseEvent
click-previewTriggered when preview image is clickedSame as after-read
click-reuploadTriggered when overwrite upload is clickedSame as after-read
close-previewTriggered when full-screen image preview is closed-
deleteTriggered when file preview is deletedSame as after-read

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