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

2024-07-12 1686阅读

实现效果:

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

调用后端接口后,后端返回的数据:vue3中antd上传图片组件及回显

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

  
    
      
        
        上传
      
    
    
      
    
  


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()
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([
 
]);
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)
  }
};


/* 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;
}

2.页面中使用

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

定义:

const props = defineProps({

  data: {} as any,

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

const fileListJson = ref([]);//封面

const fileListJson1 = ref([]);//轮播图

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

2.1 表单样式、使用组件

  
      
      
    
    
    
      
    

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  

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 
VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]