async 和 await 结合使用

关于文件上传组件:在这里使用 input 封装一个图片上传组件。原因:使用element ui 组件中的文件上传,需要使用其本身自带action :“接口地址”,来继续宁上传文件,这里只需要拿到上传文件,后处理图片数据base64格式。如果使用element ui 属性:httprequest 或者其他事件命令,在打印 console.log(fileList) 时候回循环多次打印,无比麻烦。所以采用原始 input 进行封装。

注意事项:1. async 和 await 使用,2.图片转base64位

<template>
  <div class="content">
    <div class="bttImg" @click="getChange">
      <i class="el-icon-plus"></i>
    </div>
    <div class="inputImg">
      <input
        v-show="false"
        ref="uploadImg"
        @change="getFileData($event)"
        class="uploadImg"
        multiple
        type="file"
        accept=".png, .jpg"
      />
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {};
  },
  created() {},
  mounted() {},
  methods: {
    getChange() {
      this.$refs.uploadImg.click();
    },
    getFileData(event) {
      let fileList = event.target.files;
      // async 和 await 结合 vue 中 for循环使用:注意this指向,这里将异步处理BASE64位图片,转为同步数组。
      // 结局打印数组长度为 0,而数组有数据
      let that = this;
      let ImgData = [];
      async function getters() {
        for (let i = 0; i < fileList.length; i++) {
          let res = await that.getBase64(fileList[i]);
          ImgData.push({
            name: fileList[i].name,
            fileValue: res,
          });
        }
        that.$emit("getFile", ImgData);
      }
      getters();
    },

    getBase64(file) {
      return new Promise(function (resolve, reject) {
        let reader = new FileReader();
        let imgResult = "";
        reader.readAsDataURL(file);
        reader.onload = function () {
          let data = reader.result.split(",");
          imgResult = data[1];
        };
        reader.onerror = function (err) {
          reject(err);
        };
        reader.onloadend = function () {
          resolve(imgResult);
        };
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.img-wrap {
  .title {
    line-height: 30px;
  }
  .content {
    display: flex;
    flex-wrap: wrap;

    .img-content {
      width: 170px;
      height: 150px;
      margin-bottom: 10px;
      position: relative;
      .el-icon-remove-outline {
        position: absolute;
        top: -15px;
        right: 0px;
        color: red;
        cursor: pointer;
        font-size: 25px;
      }
      .base-img {
        width: 150px;
        height: 150px;
        border-radius: 8px;
        margin-right: 10px;
      }
    }
  }
  .bttImg {
    background-color: #fbfdff;
    border: 1px dashed #c0ccda;
    border-radius: 6px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    width: 148px;
    height: 148px;
    line-height: 146px;
    vertical-align: top;
    text-align: center;
    .el-icon-plus {
      font-size: 28px;
      color: #8c939d;
    }
    .inputImg {
      display: none;
    }
    .uploadImg {
      display: none;
    }
  }
}
</style>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值