图片文件File转成base64

上传文件:

<el-form-item label="服务图标:" prop="serviceName" class="formItem">
   <el-button size="mini" type="reset" icon="el-icon-plus" @click="$refs.serviceImgDom.value = ''; $refs.serviceImgDom.click()"></el-button>
   <img :src="serviceIcon" style="width: 60px;">                        
   <input type="file" ref="serviceImgDom" style="display: none;" accept=".png;.jpeg;.svg;" @change="imgUpload">
</el-form-item>

图片文件转成base64:

imgUpload(e) {            
    let file = e.target.files[0];
    var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => { 
       console.log(reader.result); // base64
       this.serviceIcon = reader.result; 
    } 
    reader.onerror = function(err) {
         console.log(err); 
    }  
},      

你可能感兴趣的:(JS,javascript,前端,vue.js)