<template>
<t-upload :headers="headers" multiple :on-select-change="selectChangeHandler"></t-upload>
</template>
<script setup>
import { ref } from 'vue';
import { uploadImages } from '@/api/upload';
const headers = ref({
Authorization: 'Bearer your_token',
'X-Requested-With': null,
});
const selectChangeHandler = async (fileList) => {
console.info('文件选择变化', fileList[0]);
// 文件上传接口
const res = await uploadImages(fileList[0]);
console.log(res);
};
</script>
// 导入Result类型来自'@/api/model/userModel'
import type { Result } from '@/api/model/userModel';
import { request } from '@/utils/request';
// 上传图片
export function uploadImages(data: any) {
// 创建一个FormData对象
const formData = new FormData();
// 将data参数添加到formData中
formData.append('file', data);
// 发送post请求,并返回Result类型结果
return request.post<Result>({
// 请求地址
url: '/upload',
// 请求数据
data: formData,
// 请求头
headers: {
'Content-Type': 'multipart/form-data',
},
});
}