当前位置:网站首页>a-upload上传图片

a-upload上传图片

2022-08-11 10:43:00 Mr.Meng_95

a-upload上传图片

<a-upload
  list-type="picture-card"
  :file-list="fileList"
  :beforeUpload="beforeUploadMains"
  @change="handleChangeImg"
  :showUploadList="{
    
    showPreviewIcon: false
  }"
>
  <div v-if="fileList.length < 2">
  // 确定上传图片数量
    <a-icon type="plus" />
  </div>
</a-upload>

fileList: [],

beforeUploadMains(file, e) {
     
  let isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'|| file.type === 'image/jpg';
   if (!isJpgOrPng) {
    
     this.$message.error('格式错误,只能上传jpg、jpeg、png');
     return reject(false);
   }
   return false; // 阻止upload默认上传事件
 },
 handleChangeImg({
     fileList }) {
    
   this.fileList = fileList;
   console.log(this.fileList);
 },

效果

在这里插入图片描述

按钮上传

<a-upload
  list-type="picture"
  :fileList="fileList"
  :beforeUpload="beforeUploadMains"
  @change="handleChange"
>
  <a-button type="primary" @click="addmsg">新增</a-button>
</a-upload>

fileList: [],

beforeUploadMains(file, e) {
     
 let isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'|| file.type === 'image/jpg';
  if (!isJpgOrPng) {
    
    this.$message.error('格式错误,只能上传jpg、jpeg、png');
    return reject(false);
  }
  return false; // 阻止upload默认上传事件
},
handleChange(info) {
    
  // 上传图片api
  console.log(info);
},
原网站

版权声明
本文为[Mr.Meng_95]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_30351747/article/details/126243879