Skip to content

Upload 文件上传 ​

文件上传组件,基于 Vant Uploader + Field 组件实现。

类型标识 ​

  • t: 'upload'

额外 Props ​

属性名类型默认值描述
uploadPropsPartial<UploaderProps>-VanUploader 组件的属性
uploadOnobject{}VanUploader 组件的事件
uploadSlotsUploadSlots-VanUploader 组件的插槽
afterRead(file, detail, option) => void-文件读取完毕后执行的回调
uploadRequest(option) => Promise<any> | void-文件上传方法
uploadingMessagestring | Function'上传中'上传中的提示
failedMessagestring | Function-失败的提示

UploadSlots ​

插槽名绑定值描述
defaultCommonSlotsProps自定义上传区域
previewDeleteCommonSlotsProps自定义删除按钮
previewCover{ item } & CommonSlotsProps自定义覆盖在预览区域上方的内容

UploadOption ​

参数类型描述
fileFileInfo当前选中的第一个文件
filesFileInfo[]多选时所有文件
success(file, res) => void成功回调
fail(file, res) => void失败回调

Slots ​

支持所有 CommonSlots 插槽。

示例 ​

基础用法 ​

typescript
const formConfig = defineOption([
    {
        field: 'files',
        t: 'upload',
        label: '附件',
        uploadProps: {
            multiple: true,
            maxCount: 5,
            accept: '.jpg,.jpeg,.png,.gif',
        },
    },
]);

自定义上传请求 ​

typescript
const formConfig = defineOption([
    {
        field: 'files',
        t: 'upload',
        label: '附件',
        uploadRequest: ({ file, files, success, fail }) => {
            const formData = new FormData();
            formData.append('file', file.file as Blob);
            fetch('/api/upload', {
                method: 'POST',
                body: formData,
            })
                .then(res => res.json())
                .then(res => {
                    file.url = res.url;
                    success(file, res);
                })
                .catch(err => fail(file, err));
        },
    },
]);

图片预览模式 ​

typescript
const formConfig = defineOption([
    {
        field: 'images',
        t: 'upload',
        label: '图片',
        uploadProps: {
            multiple: true,
            maxCount: 9,
            uploadIcon: 'photograph',
        },
        afterRead: (file, detail, option) => {
            console.log('文件已读取:', file);
        },
    },
]);

自定义上传区域 ​

tsx
const formConfig = defineOption([
    {
        field: 'files',
        t: 'upload',
        label: '附件',
        uploadSlots: {
            default: () => (
                <VanButton icon="plus" type="primary">
                    上传文件
                </VanButton>
            ),
        },
    },
]);