vue中将图片转换为beat64格式

简单的上传注册图片,将图片转换为beat64格式 存储在后端的数据库中 ,便于访问.

页面
这里直接调用了 element-ui中的上传组件,其中imageUrl用于存储图片的信息
action 不给地址 先不直接上传
<el-form-item label="头像">
<el-upload class="avatar-uploader" action="none" :show-file-list="false" :on-change="onChange">
<img v-if="imageUrl" :src="imageUrl" class="avatar" />
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
</el-form-item>

方法
这里用 onChange file是上传到文件信息,fileList是文件列表
this.ruleForm.image是直接上传数据库的数据

onChange(file, fileList) {
fileList = [] // 清空列表
var reader = new FileReader()
reader.readAsDataURL(file.raw)
reader.onload = () => {
console.log('file 转 base64结果:' + reader.result)
this.ruleForm.image = reader.result
}
reader.onerror = function (error) {
console.log('Error: ', error)
}
this.imageUrl = URL.createObjectURL(file.raw)
},

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容