vue 循环上传图片 点击查看大图

这是一个关于前端图片上传和预览功能的实现。通过循环遍历数组对象,使用Element UI的`el-upload`组件进行图片上传,并在上传前进行压缩。同时,实现了点击图片查看大图的功能。代码中包含了上传前的图片压缩、上传过程的加载提示以及上传成功后的图片链接更新。
摘要由CSDN通过智能技术生成
<template>
    <div> 
        <div  v-for="(item,index) in  picture"  :key="item.id" class="box_uploader">
             <div @click="gainId(index)">
                  <el-upload
                         class="uploader_diyi"
                        action="123"
                        :show-file-list="false"
                        :http-request="upload"//利用自己的方法上传
                        :before-upload="beforeAvatarUpload">//上传前压缩一下照片
                         <div class="zhuanquanquan">
                              <i  class="el-icon-plus avatar-uploader-icon"></i>
                               <p>{{item.title}}</p>
                        </div>
                    </el-upload>
                      <div  class="img_up">
                         <el-image 
                            v-if=" item.pic != ''"  //不等于空的时候 显示图片
                             style="width: 100%; height: 100%"
                             :src="GLOBAL.BASE_URL + item.pic" //‘GLOBAL.BASE_URL (http://192.68.1.1...)’这个是我写的一个全局域名可以忽略
                              :preview-src-list="getImgList(index)">
                        </el-image>
                       </div>
                </div>
           </div>
    </div>
</template>

<script>
    export default {
          data() {
                return {
                      srcList:[],//点击看大图
                      currentId:'',//下标
                       picture:[
                            {id:0,title:'门头照片',pic:''},
                            {id:1,title:'前台照片',pic:''},
                            {id:2,title:'团队照片',pic:''},
                        ],
          },
           methods: {
             //通过点击获得下标位置
              gainId(i){
                  this.currentId = i 
              },
                   //上传照片
              upload(file){
                   const loading = this.$loading({
                        lock: true,
                        text: '上传中...',
                        spinner: 'el-icon-loading',
                        background: 'rgba(0, 0, 0, 0.7)'
                    });
                const formData = new FormData();
                formData.append('file',file.file);
                formData.append('end_of_lifeVehiclesId',this.currentId);
                formData.append('w',500);
                formData.append('h',500);
                var this_ = this
                  //this.changeData(),是我写的一个全局调用ajax的方法  可以直接写  axios.post()
                 this.changeData('upload/img',formData,function(res){
                    loading.close();
                    this_.picture[this_.currentId].pic = res.data.data //接口返回的图片链接 
                    this_.srcList[this_.currentId] = res.data.data
                })
              
            },
                  //以此打开查看大图
                  getImgList(index){
                        let arr = JSON.parse(JSON.stringify(this.srcList))
                        for(let i=0;i<index;i++){
                            arr.push(arr[0]);
                            arr.splice(0,1);
                      }
                    return arr
                },
            //视频上传前进行压缩
             beforeAvatarUpload(file) {
                return new Promise((resolve, reject) => {
                       compress(file).then(res=>{ // 压缩方法在后面 
                        resolve(res)
                       })
                    }).then(res=>{
                        if(res == 0) {
                            this.$message({showClose: true,duration:1500,message: '图片大小不能小于15像素',type: 'error'});
                           return reject()
                        }else{
                            return res
                        }
                    })
             },
          }
    }
</script>

<style lang="scss" scoped>

</style>

这里是写了一个数组对象进行循环的,因为就3张。所有没有写3个上传的方法
利用这样的方法进行了 循环上传并且以此点开大图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值