el-upload上传照片转base64给后台

 效果图:

 上传单张照片,这里的dataForm.companyInfo.creditImage为字符串

 <el-upload action="https://jsonplaceholder.typicode.com/posts/" :show-file-list="false" :on-change="beforeAvatarUpload">
      <img v-if="dataForm.companyInfo.creditImage" :src="dataForm.companyInfo.creditImage" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
    // 转换成base64方法
    getBase64(file) {
      return new Promise(function (resolve, reject) {
        const reader = new FileReader()
        let imgResult = ''
        reader.readAsDataURL(file)
        reader.onload = function () {
          imgResult = reader.result
        }
        reader.onerror = function (error) {
          reject(error)
        }
        reader.onloadend = function () {
          resolve(imgResult)
        }
      })
    },
    beforeAvatarUpload(file) {
      this.getBase64(file.raw).then(res => {
        this.dataForm.companyInfo.creditImage = res
      })
    },

 转码之后的数据格式:

<style scoped>
.avatar-uploader-icon {
  border: 1px dashed #d9d9d9;
  font-size: 28px;
  color: #8c939d;
  width: 300px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 300px;
  height: 178px;
  display: block;
}
</style>

上传多张照片,这里的this.dataForm.intors为数组

on-preview点击文件列表中已上传的文件时的钩子function(file)
on-remove文件列表移除文件时的钩子function(file, fileList)
on-change文件状态改变时的钩子,添加文件、上传成功和上传失败时都会被调用function(file, fileList)
list-type文件列表的类型stringtext/picture/picture-cardtext
file-list上传的文件列表, 例如: [{name: 'food.jpg', url: 'https://xxx.cdn.com/xxx.jpg'}]array[]
multiple是否支持多选文件boolean
  <el-col :span="22">
          <el-form-item label="赛事介绍">
            <el-upload multiple action="https://jsonplaceholder.typicode.com/posts/" list-type="picture-card" :file-list="fileList" :on-remove="handleUploadRemove" :on-preview="handlePictureCardPreview" :on-change="handleRemove">
              <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog  append-to-body :visible.sync="dialogVisible">
              <img v-if="dataForm.intors" width="100%" :src="dataForm.intors" alt="">
            </el-dialog>
          </el-form-item>
</el-col>
 dialogVisible: false,
 fileList: [],

回显:

   this.dataForm.intors.forEach(element => {
          let obj = new Object();
          obj.url = element;
          this.fileList.push(obj);
   });
//上传
 handleRemove(file, fileList) {
     var newomtors = []
      for (let item of fileList) {
        this.getBase64(item.raw).then((res) => {
          newomtors.push(res)
          let arrintors = this.fileList.map(item => {
            return item.url;
          });
          //arrintors为旧图片newomtors为新添加的图片合并到一个数组里传给后台
          let all = [...arrintors, ...newomtors]
          this.dataForm.intors = all
        });
      }
},
//预览
 handlePictureCardPreview(file) {
      this.dataForm.intors = file.url;
      this.dialogVisible = true;
},
 // 删除
    handleUploadRemove(file, fileList) {
     const index = this.dataForm.intors.findIndex(item => {return item === file.url})
     this.dataForm.intors.splice(index, 1)
},

补充:保存或者取消的时候页面刷新重新获取图片

//APP.vue页面
<template>
  <div id="app">
    <router-view v-if="isRouterAlive" />
  </div>
</template>
<script>
export default {
  name: 'App',
  provide() {
    return {
      reload: this.reload
    }
  },
  data() {
    return {
      isRouterAlive: true
    }
  },
  methods: {
    reload() {
      this.isRouterAlive = false;
      this.$nextTick(function () {
        this.isRouterAlive = true;
      })
    }
  },
}
</script>
//要刷新的页面
export default {
  inject: ['reload'], //引入
}
//使用
this.reload();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值