签入版本
This commit is contained in:
@@ -363,35 +363,42 @@ file: <二进制图片>
|
||||
|
||||
## 前端集成
|
||||
|
||||
### Element Plus 上传组件
|
||||
### Ant Design Upload 组件
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<el-upload
|
||||
<a-upload
|
||||
:action="'/api/upload/uploadFile'"
|
||||
:headers="{ Authorization: `Bearer ${token}` }"
|
||||
:on-success="handleUploadSuccess"
|
||||
:before-upload="beforeUpload"
|
||||
@change="handleUploadSuccess"
|
||||
>
|
||||
<el-button type="primary">上传文件</el-button>
|
||||
</el-upload>
|
||||
<a-button type="primary">上传文件</a-button>
|
||||
</a-upload>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { message } from 'ant-design-vue';
|
||||
|
||||
const token = localStorage.getItem('access_token');
|
||||
|
||||
// 返回 false 阻止上传
|
||||
const beforeUpload = (file) => {
|
||||
const isLt10M = file.size / 1024 / 1024 < 10;
|
||||
if (!isLt10M) {
|
||||
ElMessage.error('文件大小不能超过 10MB');
|
||||
message.error('文件大小不能超过 10MB');
|
||||
return false;
|
||||
}
|
||||
return isLt10M;
|
||||
return true;
|
||||
};
|
||||
|
||||
const handleUploadSuccess = (response) => {
|
||||
// response.data.fileUrl 是完整 URL
|
||||
// file.response 是上传接口的返回体
|
||||
const handleUploadSuccess = ({ file }) => {
|
||||
// file.response.data.fileUrl 是完整 URL
|
||||
// 提交表单时使用此 URL
|
||||
form.value.avatar = response.data.fileUrl;
|
||||
if (file.response?.code === 0) {
|
||||
form.value.avatar = file.response.data.fileUrl;
|
||||
}
|
||||
};
|
||||
</script>
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user