Ant Design Vue + ts实现文件上传

话不多说,直接上代码

<template>
    <a-form @submit.prevent="handleSubmit" :label-col="{ span: 6 }" :wrapper-col="{ span: 16 }" style="margin-top: 35px;">
        <a-form-item label="授权文件">
            <a-upload :before-upload="beforeUpload" :file-list="fileList" :on-remove="handleRemove">
                <a-button> <CloudUploadOutlined /> 点击上传 </a-button>
            </a-upload>
        </a-form-item>
        <a-form-item :wrapper-col="{ span: 6, offset: 18 }">
            <a-button type="primary" html-type="submit" style="width: 80px ;">提交</a-button>
        </a-form-item>
    </a-form>
</template>

<script lang="ts" setup>
import { onMounted, ref } from 'vue';
import { message } from 'ant-design-vue';

const emit = defineEmits(['close']);

const fileList = ref<File[]>([]);
let file: File | null = null;

const beforeUpload = (uploadFile: File) => {
    fileList.value = [uploadFile];
    file = uploadFile;
    return false; // 阻止自动上传
};

const handleRemove = () => {
    fileList.value = [];
    file = null;
};

const handleSubmit = async () => {
    if (!file) {
        message.warn('请先上传文件!');
        return;
    }
    const params = {}; // 根据你的实际情况设置参数
    const body = {
        /*
         *参数
         */
    };
    try {
        //	发起接口请求 const response = await XXX
        if (response) {
            emit('close', true);
            message.success('上传成功!');
        }
    } catch (error) {
        message.error('上传失败!');
    }
};
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值