vue3中antd上传图片组件及回显

server/2024/10/20 8:52:28/

实现效果:

调用后端接口后,后端返回的数据:

1.在项目components/base下新建UploadNew.vue文件(上传图片公共组件)

<template><div class="clearfix"><a-uploadv-model:file-list="fileList"action="/platform-gateway/platform-file/security/chain"list-type="picture-card":headers="headers"@preview="handlePreview"@change="handleChange" ><div v-if="fileList.length < props.limit"><plus-outlined /><div style="margin-top: 8px">上传</div></div></a-upload><a-modal :open="previewVisible" :title="previewTitle" :footer="null" @cancel="handleCancel"><img alt="example" style="width: 100%" :src="previewImage" /></a-modal></div>
</template>
<script lang="ts" setup>
import { PlusOutlined } from '@ant-design/icons-vue';
import { ref } from 'vue';
import type { UploadChangeParam, UploadProps } from 'ant-design-vue';
import { HttpClientUtils } from '../../configure/http/httpClientUtils'interface Props {accept: string, // 上传文件的格式,limit: number,//上传图片数量fileListJson :UploadProps['fileList']
}
const props = defineProps<Props>()
function getBase64(file: File) {return new Promise((resolve, reject) => {const reader = new FileReader();reader.readAsDataURL(file);reader.onload = () => resolve(reader.result);reader.onerror = error => reject(error);});
}const previewVisible = ref(false);
const previewImage = ref('');
const previewTitle = ref('');
//调用后端接口请求头
const headers = {authorization: HttpClientUtils.getToken(),platform: 'WEB',serviceCode: 'athletics-service'
}
const $emit = defineEmits(["uploadDone"]) 
const fileList = ref<UploadProps['fileList']>([]);
if (props.fileListJson) {fileList.value = props.fileListJson
}
const handleCancel = () => {previewVisible.value = false;previewTitle.value = '';
};const handlePreview = async (file: UploadProps['fileList'][number]) => {if (!file.url && !file.preview) {file.preview = (await getBase64(file.originFileObj)) as string;}previewImage.value = file.url || file.preview;previewVisible.value = true;previewTitle.value = file.name || file.url.substring(file.url.lastIndexOf('/') + 1);
};const handleChange = (info: UploadChangeParam) => {if (info.file.status === 'done') {//   imageId.value = 'http://192.168.5.13/platform-gateway/platform-file/security/chain?fileName=' + info.file.response.data[0] + '&serviceCode=athletics-service'$emit("uploadDone", info.fileList)}
};
</script><style scoped>
/* you can make up upload button and sample style by using stylesheets */
.ant-upload-select-picture-card i {font-size: 32px;color: #999;
}.ant-upload-select-picture-card .ant-upload-text {margin-top: 8px;color: #666;
}
</style>

2.页面中使用

先引入:import AntdUploadFile  from "@/components/base/UploadNew.vue";

定义:

const props = defineProps({

  data: {} as any,

})//点编辑时父组件传入的回显数据

const fileListJson = ref<UploadProps['fileList']>([]);//封面

const fileListJson1 = ref<UploadProps['fileList']>([]);//轮播图

const formData = reactive({venue: {headerUrl:props.data?.headerUrl,bannerUrl:props.data?.bannerUrl,},
})

2.1 表单样式、使用组件

  <a-form-item :name="['venue', 'headerUrl']" label="封面图" :rules="[{ required: true }]"><AntdUploadFile:fileListJson="fileListJson":limit="1" accept="" type="frontIdcard" @upload-load="onUploading"@upload-done="onUploadDone" ></AntdUploadFile></a-form-item><a-form-item :name="['venue', 'bannerUrl']" label="场馆轮播" :rules="[{ required: true }]"><AntdUploadFile:limit="4" accept="" :fileListJson="fileListJson1"type="frontIdcard1" @upload-load="onUploading1"@upload-done="onUploadDone1" ></AntdUploadFile></a-form-item>

2.2 上传图片成功 ,点保存后传给后端

// 封面图片上传成功(单个图)
const onUploadDone = (fileList: any) => {// 文件上传成功后返回的文件信息 type,是为了一个页面引用多少文件上传作的区分if (fileList.length) {formData.venue.headerUrl= fileList[0].response.data}console.log( formData.venue.headerUrl,"上传成功后的参数 ", fileList);
}
// 轮播图片上传成功(多张图)
const onUploadDone1 = (fileList: any) => {// 文件上传成功后返回的文件信息 type,是为了一个页面引用多少文件上传作的区分let bannerUrl = []if (fileList.length) {for (let i = 0; i < fileList.length; i++) {if (fileList[i].response) {bannerUrl.push({"url":fileList[i].response.data,})} else {//将fileName= 后面的数据和&前的数据截取push到url后,转为json字符串传给后端const index = fileList[i].url.indexOf('fileName=')let newIndex: anyif (index !== -1) {const start = fileList[i].url.slice(index + 'fileName='.length)const endIndex = start.indexOf('&')if (endIndex !== -1) {newIndex = start.slice(0,endIndex)}}bannerUrl.push({"url": newIndex,})}     }}formData.venue.bannerUrl =JSON.stringify(bannerUrl) 
}

2.3 点编辑时回显(因本接口后端返回的数据需要拼接,可根据自行情况修改)

if (props.data.bannerUrl||props.data.headerUrl) {fileListJson.value.push({"url":'http://platform-file/security/chain?fileName=' + props.data.headerUrl + '&serviceCode=athletics-service',})const bannerList =  JSON.parse(props.data.bannerUrl)//json字符串转为数组console.log(bannerList,'里面有这个图片吗')for (let i = 0;i< bannerList.length;i++) {fileListJson1.value.push({"url":'后端返回的地址',})}}


http://www.ppmy.cn/server/59743.html

相关文章

JupyterNotebook中导出当前环境,并存储为requirements.txt

​使用Anaconda管理Python环境时&#xff0c;可以轻松地导出环境配置&#xff0c;以便在其他机器或环境中重新创建相同的环境。可以通过生成一个environment.yml文件实现的&#xff0c;该文件包含了环境中安装的所有包及其版本。但是&#xff0c;常常在一些课程中JupyterNotebo…

RabbitMQ 迁移

文章目录 1. 导出配置2. 导入配置3. 导出和导入定义&#xff08;如果不需要消息&#xff09;导出定义导入定义 注意事项参考文档 要将 RabbitMQ 的配置&#xff08;包括vhost、exchange等&#xff09;从一个实例迁移到另一个实例&#xff0c;您可以遵循以下步骤&#xff1a; 1.…

详解yolov5的网络结构

转载自文章 网络结构图&#xff08;简易版和详细版&#xff09; 此图是博主的老师&#xff0c;杜老师的图 网络框架介绍 前言&#xff1a; YOLOv5是一种基于轻量级卷积神经网络&#xff08;CNN&#xff09;的目标检测算法&#xff0c;整体可以分为三个部分&#xff0c; ba…

UDP协议介绍和作用

什么是UDP? UDP是User Datagram Protocol的简称&#xff0c;中文名是用户数据报协议&#xff0c;是OSI参考模型中的传输层协议&#xff0c;它是一种无连接的传输层协议&#xff0c;提供面向事务的简单不可靠信息传送服务。 UDP的正式规范是IETF RFC768。UDP在IP报文的协议号是…

苹果电脑压缩软件哪个好用一些? mac电脑用什么压缩软件 mac电脑压缩文件怎么设置密码

压缩软件是Mac电脑必不可少的工具&#xff0c;虽然Mac系统自带了一款“归档实用工具”&#xff0c;但是其功能实在匮乏&#xff0c;若你需要加密压缩文件或者把文件压缩成指定格式&#xff0c;那么该工具无法满足你的需求。Mac用户应该怎么选择压缩软件呢&#xff1f;本文就来告…

PostgreSql中的JSON数据类型

PostgreSQL 提供了两种 JSON 数据类型&#xff1a;JSON 以及 JSONB。这两种类型主要的区别在于数据存储格式&#xff0c;JSONB 使用二进制格式存储数据&#xff0c;更易于处理。 PostgreSQL 推荐优先选择 JSONB 数据类型。 两种数据类型之间的区别&#xff1a; 功能JSONJSONB存…

AR增强现实汽车装配仿真培训系统开发降低投入费用

随着互联网的无处不在&#xff0c;AR增强现实技术正逐步融入我们生活的每一个角落。深圳华锐视点作为一家引领行业潮流的AR内容开发的技术型公司&#xff0c;正以其卓越的技术实力和专业的服务团队&#xff0c;推动着国内AR技术向更加成熟和多元化的方向迈进。 深圳华锐视点提供…

Windows图形界面(GUI)-SDK-C/C++ - 组合框(ComboBox)

公开视频 -> 链接点击跳转公开课程博客首页 -> 链接点击跳转博客主页 目录 组合框(ComboBox) 控件样式 创建控件 初始控件 消息处理 示例代码 组合框(ComboBox) 控件样式 组合框是一个包含编辑框和列表框的控件。用户可以从下拉列表中选择一项&#xff0c;或者直…