Skip to content

Uploader 上传

uploader

规则

基础示例

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

Props 配置示例

单图片上传

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

多图片上传

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

文件上传

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

上传前验证

js
const rule = {
    type: 'uploader',
    title: '头像',
    field: 'avatar',
    value: [],
    props: {
        action: '/upload.php',
        maxCount: 1,
        accept: 'image/*',
        maxSize: 2 * 1024 * 1024, // 2MB
        beforeRead: (file) => {
            // 验证文件大小
            if (file.size > 2 * 1024 * 1024) {
                alert('图片大小不能超过 2MB!');
                return false;
            }
            return true;
        },
        onSuccess(res, file) {
            file.url = res.url;
        }
    }
}

Events 事件示例

监听上传事件

js
const rule = {
    type: 'uploader',
    title: '图片上传',
    field: 'pic',
    value: [],
    props: {
        action: '/upload.php',
        accept: 'image/*',
        onSuccess(res, file) {
            file.url = res.url;
        },
        afterRead: (file) => {
            console.log('文件读取完成:', file);
        },
        beforeRead: (file) => {
            console.log('文件读取前:', file);
            return true;
        },
        beforeDelete: (file) => {
            console.log('删除前:', file);
            return true;
        },
    },
    on: {
        oversize: (file) => {
            console.log('文件大小超过限制:', file);
        },
        'click-upload': (event) => {
            console.log('点击上传区域:', event);
        },
        delete: (file) => {
            console.log('删除文件:', file);
        },
    },
}

完整配置项:Vant_Uploader

value :Array | String

注意

文件上传成功后需要通过 onSuccess 回调, 将接口返回内容中的 url 赋值给 file.url.否则表单获取不到组件的数据

Props

参数说明类型默认值
accept允许上传的文件类型,详细说明stringimage/*
name标识符,通常为一个唯一的字符串或数字,可以在回调函数的第二项参数中获取number | string-
preview-size预览图和上传区域的尺寸,默认单位为 pxnumber | string | Array80px
preview-image是否在上传完成后展示预览图booleantrue
preview-full-image是否在点击预览图后展示全屏图片预览booleantrue
preview-options全屏图片预览的配置项,可选值见 ImagePreviewobject-
multiple是否开启图片多选,部分安卓机型不支持booleanfalse
disabled是否禁用文件上传booleanfalse
readonly是否将上传区域设置为只读状态booleanfalse
deletable是否展示删除按钮booleantrue
reupload是否开启覆盖上传,开启后会关闭图片预览booleanfalse
show-upload是否展示上传区域booleantrue
lazy-load是否开启图片懒加载,须配合 Lazyload 组件使用booleanfalse
capture图片选取模式,可选值为 camera (直接调起摄像头)string-
after-read文件读取完成后的回调函数Function-
before-read文件读取前的回调函数,返回 false 可终止文件读取, 支持返回 PromiseFunction-
before-delete文件删除前的回调函数,返回 false 可终止文件读取, 支持返回 PromiseFunction-
max-size文件大小限制,单位为 bytenumber | string | (file: File) => booleanInfinity
max-count文件上传数量限制number | stringInfinity
result-type文件读取结果类型,可选值为 file textstringdataUrl
upload-text上传区域文字提示string-
image-fit预览图裁剪模式,可选值见 Image 组件stringcover
upload-icon上传区域图标名称或图片链接,等同于 Icon 组件的 name 属性stringphotograph

Events

事件名说明回调参数
oversize文件大小超过限制时触发after-read
click-upload点击上传区域时触发event: MouseEvent
click-preview点击预览图时触发after-read
click-reupload点击覆盖上传时触发after-read
close-preview关闭全屏图片预览时触发-
delete删除文件预览时触发after-read

FormCreate 是一个开源项目,基于 MIT 许可证发布,欢迎个人和企业用户免费使用