<template>
<div>
<a href="javascript:void(0);" @change="addImage" class="a">
<input type="file" class="fileopen" />上传图片
</a>
<img :src="imgsrc" alt class="imgview" accept="image/png, image/jpeg, image/gif, image/jpg" />
</div>
</template> <script>
export default {
data() {
return {
imgsrc: ""
};
},
methods: {
addImage() {
var input = document.querySelector("input");
//1. 拿到fileinput里面的文件, 这个file是一个file对象, file对象不能直接展示的
var file = input.files[0]; //2. 读取文件,成功img标签可以直接使用的格式
//FileReader类就是专门用来读文件的
var reader = new FileReader();
window.console.log(file); //3. 开始读文件
//readAsDataURL: dataurl它的本质就是图片的二进制数据, 进行base64加密后形成的一个字符串, 这个字符串可以直接作用img标签的图片资源使用 reader.readAsDataURL(file);
let _this = this;
//4. 因为文件读取是一个耗时操作, 所以它在回调函数中,才能够拿到读取的结果
reader.onload = function() {
window.console.log(reader.result);
//直接使用读取的结果
_this.imgsrc = reader.result;
};
_this.imgsrc = file;
}
}
};
</script> <style lang="less" scoped>
.imgview {
width: 150px;
height: 150px;
border-radius: 50%;
border: 1px solid red;
}
.a {
position: relative;
display: block;
text-decoration: none;
color: aqua;
}
.fileopen {
position: absolute;
left: 0;
top: 0;
opacity: 0;
filter: alpha(opacity=0);
width: 64px;
overflow: hidden;
}
</style>

  

最新文章

  1. gulp教程之gulp-uglify
  2. position&amp;containing block
  3. [LintCode] Valid Parentheses 验证括号
  4. 一张思维导图说明jQuery的AJAX请求机制
  5. Android应用中-更新提示显示红点的方案
  6. Linq的简单查询
  7. MySQL 远程访问开启
  8. QtCreator调试传入运行参数
  9. 利用FreeMarker静态化网页
  10. sql 各种格式
  11. 安装mongodb到系统服务
  12. 2016 &quot;Bird Cup&quot; ICPC7th@ahstu--“波导杯”安徽科技学院第七届程序设计大赛
  13. Asp.Net Core 轻松学-经常使用异步的你,可能需要看看这个文章
  14. Linux keepalived+lvs实现高可用负载均衡
  15. Maven 的这 7 个问题你思考过没有?
  16. Elastic Stack之kibana使用
  17. 【转】python编写规范——中标软件有限公司测试中心
  18. Java程序员秋招面经大合集(BAT美团网易小米华为中兴等)
  19. HDU 5468 Puzzled Elena (dfs + 莫比乌斯反演)
  20. linux buff/cache释放

热门文章

  1. .Net Core 使用 NPOI 导入Excel
  2. WPF之DataTemplateSelector的运用
  3. CSS入门(定位之浮动定位、伪类之鼠标悬停、光标修改和透明度修改和列表样式)
  4. ABAP分享三 批量上传数据到内表简单示例
  5. 基于django的个人博客网站建立(五)
  6. Qt的诞生和本质
  7. mysql分组报错Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column
  8. 解决安装tensorflow-gpu失败:Command &quot;python setup.py egg_info&quot;failed with error code 10 in.....
  9. java之List接口(单列集合)
  10. 【安富莱】V6,V5开发板用户手册,重在BSP驱动包设计方法,HAL库的框架学习,授人以渔(2019-11-04)