js将上传的照片转化为base64

js将上传的照片转化为base64

<el-upload
	class="avatar-uploader"
	action=""
	:show-file-list="false"
	:auto-upload="false"
	:on-change="changeFile" v-loading="loading">
	<img id="giftImg" v-if="imageUrl" :src="imageUrl" class="avatar" style="width: 20%;height: 20%">
	<el-button v-else slot="trigger" size="small" type="primary">选取文件</el-button>
</el-upload>
<p>请选择jpg, png格式的文件上传, 文件不大于3M</p>
changeFile(file, fileList) {
   this.loading=true;
    var reader = new FileReader();
    reader.readAsDataURL(file.raw);
    reader.onload = function (e) {
      this.result // 这个就是base64编码了
      this.imageUrl = this.result;
    }
},

在这里用的element 的el-upload ,

//如果报错说base64使用了多余的字符之类的就分隔开
this.result.split(",")[1]
发布了34 篇原创文章 · 获赞 5 · 访问量 2248

猜你喜欢

转载自blog.csdn.net/tanfei_/article/details/103948394