多图上传异步转换同步-图片转换ba64位数据

							**图片转换ba64位数据**
多图上传reader.onloadend异步处理ba64位事件---转换成同步处理事件-vue 组件iview图标
复制代码
<template>     
    <div class="uploadImgFile-box"><Icon type="md-cloud-upload" class="uploadImgFile-icon" />上传图片
        <input type="file" class="uploadImgFile" multiple="multiple" @change="uploadImgFile" accept="image/jpeg,image/png,image/gif" />
    </div> 
</template>
<script>
export default {  
  data() {
    return {     
      imgIndex:0,
      imgStyle:true,
      imgArr:[]
    }
  },     
  methods: { 
     loadImageAsync (filesdata) {
            return new Promise( function( resolve, reject){
              var reader = new FileReader();
              reader.readAsDataURL(filesdata); // 读出 base64 
              reader.onloadend = function(){
                resolve(reader);
              };
              reader.onerror = function () {
                reject(new Error("could not load image at ") );
              };            
          });
        },
    uploadImgFile(event){     
      const that = this;
      const eventdata = event.target.files;
            that.imgArr = [];
            that.imgIndex = 0;
            that.imageAsync(eventdata)
    },
    imageAsync(event){
      const that = this;
      const filesArray = event;
      const filesLen = filesArray.length;      
      if(that.imgIndex < filesLen ){       
        that.loadImageAsync(filesArray[that.imgIndex]).then( function (value) {
                  that.imgArr.push({
                    index : that.imgIndex,
                    url : value.result
                  })  
              that.imgIndex += 1;   
              that.imageAsync(filesArray)                          
        })
      }else{
        that.imgStyle = false;
        console.log(that.imgArr)
       // that.imageSuccessCBK(that.imgArr)
      }
    }
  }
}
</script>

<style scoped>
.uploadImgFile-box{  
  background-color: rgb(24, 144, 255);
  width: 88px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  position: relative;
  color: white;
  border-radius: 3px;
}
.uploadImgFile-icon{
  font-size: 16px;
  margin-right: 3px;
}
.uploadImgFile{
  position: absolute;
  right: 0px;
  top: 0px;
  z-index: 9;
  width: 100%;
  height: 100%;
  opacity: 0;
}
</style>
复制代码

转载于:https://juejin.im/post/5cb04d21e51d456e5d3dac0a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值