vue预览本地图片
2024-09-04 22:19:45
<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>
最新文章
- gulp教程之gulp-uglify
- position&;containing block
- [LintCode] Valid Parentheses 验证括号
- 一张思维导图说明jQuery的AJAX请求机制
- Android应用中-更新提示显示红点的方案
- Linq的简单查询
- MySQL 远程访问开启
- QtCreator调试传入运行参数
- 利用FreeMarker静态化网页
- sql 各种格式
- 安装mongodb到系统服务
- 2016 ";Bird Cup"; ICPC7th@ahstu--“波导杯”安徽科技学院第七届程序设计大赛
- Asp.Net Core 轻松学-经常使用异步的你,可能需要看看这个文章
- Linux keepalived+lvs实现高可用负载均衡
- Maven 的这 7 个问题你思考过没有?
- Elastic Stack之kibana使用
- 【转】python编写规范——中标软件有限公司测试中心
- Java程序员秋招面经大合集(BAT美团网易小米华为中兴等)
- HDU 5468 Puzzled Elena (dfs + 莫比乌斯反演)
- linux buff/cache释放
热门文章
- .Net Core 使用 NPOI 导入Excel
- WPF之DataTemplateSelector的运用
- CSS入门(定位之浮动定位、伪类之鼠标悬停、光标修改和透明度修改和列表样式)
- ABAP分享三 批量上传数据到内表简单示例
- 基于django的个人博客网站建立(五)
- Qt的诞生和本质
- mysql分组报错Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column
- 解决安装tensorflow-gpu失败:Command ";python setup.py egg_info";failed with error code 10 in.....
- java之List接口(单列集合)
- 【安富莱】V6,V5开发板用户手册,重在BSP驱动包设计方法,HAL库的框架学习,授人以渔(2019-11-04)