vue3 + ts + Ant Design Vue实现文件上传

<a-row type="flex">
	<a-col :span="4">上传文件:</a-col>
	<a-col style="color:#F64545">
	    <a-upload 
            v-model:file-list="fileList"
            name="file"
			:multiple="true"
			action="上传文件的接口地址"
			:headers="headers"
			@change="handleChange"
		>
			<a-button type="primary">
			    <upload-outlined></upload-outlined>
				    选择文件
			</a-button>
		</a-upload>
	</a-col>
</a-row>
// 引入保存token的文件
import { useUserStore } from "/@/store/modules/user";

interface FileItem {
	uid: string;
	name?: string;
	status?: string;
	response?: string;
	url?: string;
}
	
interface FileInfo {
	file: FileItem;
	fileList: FileItem[];
}


setup(){
    const fileList = ref([]);
    const handleChange = (info: FileInfo) => {
        
		fileList.value = info.fileList.slice(-1) // 这里限制只保留一条上传数据 如不限制可直接屏蔽

		if (info.file.status !== 'uploading') {
			console.log(info.file, info.fileList);
		}
		if (info.file.status === 'done') {
			message.success(`${info.file.name} 上传成功`);
		} else if (info.file.status === 'error') {
			message.error(`${info.file.name} 上传失败`);
		}
	};
    
    // 我的token
    const userStore = useUserStore();
	const token = userStore.getToken;
	const headers = "Bearer " + token
    return {
        fileList,
        headers: {
			authorization: headers,
		},
		handleChange
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值