首页 > 其他分享 >vue多图片上传组件

vue多图片上传组件

时间:2022-09-21 09:55:32浏览次数:53  
标签:vue return 上传 fileList data file reader 组件 type

 

 

 

 

 

<template>
  <!-- 
    上传控件 
    用法:
     <upload-widget v-model="imgUrl"></upload-widget>
  -->
  <div class="clearfix">
    <a-upload
      :action="manageApi + '/thumbController/upload.do'"
      :data="fileUrl"
      list-type="picture-card"
      :file-list="fileInfos"
      :headers="headers"
      @preview="handlePreview"
      @change="handleChange"
      :remove="handelRemove"
      accept="image/*"
      :showUploadList="{ showPreviewIcon: this.showPreviewIcon, showRemoveIcon: this.showRemoveIcon }"(显示眼睛和隐藏判断)
    >
      <a-icon type="plus" />
      <div class="ant-upload-text">Upload</div>
    </a-upload>
    <!-- 图片预览 -->
    <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
      <img alt="example" style="width: 100%; height: 100%" :src="previewImage" />
    </a-modal>
  </div>
</template>

<script>
import axios from 'axios';

/**把图片转成BASE64 */
function getBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = (error) => reject(error);
  });
}
export default {
  name: 'UploadAlbumWidget',
  model: {
    prop: 'fileList',
    event: 'change',
  },
  props: {
    //最大上传数量
    maxUploadNum: {
      type: Number,
      default: 1,
    },
    /**文件列表 */
    fileList: {
      type: [Array, String],
      default() {
        return '';
      },
    },
    type: {
      type: String,
    },
    showPreviewIcon: {
      type: Boolean,
      default() {
        return true;
      },
    },
    showRemoveIcon: {
      type: Boolean,
      default() {
        return true;
      },
    },
  },
  data() {
    return {
      headers: {}, //头
      previewVisible: false,
      previewImage: '',
      fileInfos: [], //上传文件
    };
  },
  created() {
    // this.initVModelData();
    /**默认添加验证token */
    this.headers = {
      token: this.store.user.token,
      adminToken: this.store.admin.token,
    };
  },
  methods: {
    fileUrl() {
      return {
        type: this.type,
      };
    },
    /**处理初始v-model数据 */
    initVModelData() {
      let that = this;
      console.log('initVModelData', this.fileList);
      //判断文件上传是否多个
      if (this.fileList) {
        //多文件
        that.fileList.forEach((fl, index) => {
          that.fileInfos.push({
            uid: '-' + index,
            name: '图集',
            status: 'done',
            url: process.env.VUE_APP_IMAGE_SERVER + fl.imgPath,(process.env.VUE_APP_IMAGE_SERVER =》》》》  

 

 

 

 

)
            thumbUrl: process.env.VUE_APP_IMAGE_SERVER + fl.thumbPath,
            response: {
              data: [{ id: fl.id }],
            },
          });
        });
        // }
      }
    },
    handleCancel() {
      this.previewVisible = false;
    },
    /**预览图 */
    async handlePreview(file) {
      if (!file.url && !file.preview) {
        file.preview = await getBase64(file.originFileObj);
      }
      console.log('handlePreview', file);
      this.previewImage = file.thumbUrl || file.preview;
      this.previewVisible = true;
    },

    handleChange({ fileList }) {
      this.fileInfos = fileList;
    },
    handelRemove(file) {
      console.log('handelRemove ');
      let result = axios({
        method: 'post',
        url: `thumbController/del.do`,
        data: {
          id: file.response.data[0].id,
        },
      });
      if (result.data.code == 200) {
        this.$message.success('删除成功');
      } else {
        this.$message.error(result.data.message);
        return false;
      }
    },
  },
  watch: {
    /**检测v-model数据是否发生改变 */
    fileList(val) {
      this.initVModelData();
    },
  },
};
</script>

<style scoped>
.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>

 

标签:vue,return,上传,fileList,data,file,reader,组件,type
From: https://www.cnblogs.com/book-student/p/16714566.html

相关文章

  • Vue中使用js-audio-recorder实现录音时提示:浏览器不支持getUserMedia!
    场景Vue中使用js-audio-recorder插件实现录音功能并实现上传Blob数据到SpringBoot后台接口:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/126957202上面......
  • 【AGC】崩溃服务上传mapping混淆文件报错问题
    ​【问题描述】在上传崩溃服务的混淆Mapping文件时出现下列报错,混淆Mapping文件和原生符号文件的大小限制分别是多少?​ 【解决方案】.在AGC页面手动上传这种方式支持......
  • Vue 组合式函数简介
    Vue组合式函数:export导出一个函数。函数内可以定义生命周期勾子、数据及方法,它是可复用的模块。类似Mixin混入。但比Mixin更有优势。组合式函数示例:useDemo.js impo......
  • 为什么使用 Pinia(Vuex)
    Pinia定义Pinia(Vuex?)是一个独立于组件的全局状态,每一个组件都可以读取和写入。Pinia有三个重要的概念:state、getters、actions。state等价于组件中的data、getters......
  • VueRouter 报错:inject() can only be used inside setup() or functional components
    单独创建的一个文件,封装了登录函数的业务逻辑,出现了一个警告,紧接着就是报错:说不能读取到路由的push函数。路由必须在组件里面使用,不能在ts或js文件中使用。还要注......
  • Vue3:Teleport
    TeleportVue鼓励我们通过将UI和相关行为封装到组件中来构建UI。我们可以将它们嵌套在另一个内部,以构建一个组成应用程序UI的树。然而,有时组件模板的一部分逻辑上属......
  • Vue3:状态驱动的动态 CSS
     状态驱动的动态CSS单文件组件的<style>标签可以通过v-bind这一CSS函数将CSS的值关联到动态的组件状态上 <template><divclass="box1">hello</div>......
  • 基于SSM+Vue的农产品销售系统Java农产品在线商城系统(源码调试+讲解+文档)
    ......
  • Vue3:注册组件
    注册组件组件内部<script>importBox1from"./Box1.vue"exportdefult{components:{Box1},setup(){}}</scr......
  • Vue3:Suspense
    等待异步组件时渲染一些额外的内容,让应用有更好的用户体验<suspense>组件有两个插槽。它们都只接收一个直接子节点。default插槽里的节点会尽可能展示出来。如果不能,则......