//单图上传
<template>
<div>
<div class="uploader" v-if='!dwimg'>
<van-uploader :after-read="ondwRead" accept="image/gif, image/jpeg" multiple>
<van-icon name="photograph" />
</van-uploader>
</div>
<p v-if="dwimg" class="img"><img id="img" :src="dwimg" alt="" @click='yltp(dwimg)'><b @click='htp'><van-icon name="close" /></b></p> </div>
</template>
<script>
import axios from 'axios'
import { Toast } from 'vant';
import { ImagePreview } from 'vant';
import hbsp from '@/components/rwlctbcons/hbsp'
var instance = axios.create({
headers: {'Content-Type': 'multipart/form-data'}
});
export default {
name:'hbsp',
data(){
return{
imgmurl:'',
dwimg:'',
}
},
methods:{
//传值给父组件 父组件监听v-on:dantuEvent='function()'用function接收
childhbdbimg(){
console.log('1111');
this.$emit('dantuEvent',this.dwimg)
},
//删除
htp(){
this.dwimg='';
},
//预览图片
yltp(dwimg){
ImagePreview([
dwimg
]);
},
ondwRead(file){
Toast.loading({
duration: 0,
mask: true,
forbidClick: false,
message: '上传中...'
});
//console.log(file.content);
console.log(file);//未压缩的file
let img = new Image();
img.src = file.content;
this.dwimg=file.content;
let that=this;
img.onload=function(){
that.ontpys(img);
}
//上传成功的图片
function fn(){
setTimeout(() => {
if(that.imgmurl){
that.dwimg= that.imgmurl;
console.log(that.dwimg);
that.imgmurl='';
that.childhbdbimg();
Toast.clear();
}else{
fn();
}
}, 1000);
};
fn();
}, //压缩图片的方法
ontpys(img){
let originWidth = img.width, // 压缩后的宽
originHeight=img.height,
maxWidth = 400, maxHeight = 400,
quality = 0.8, // 压缩质量
canvas = document.createElement('canvas'),
drawer = canvas.getContext("2d");
canvas.width = maxWidth;
canvas.height = originHeight/originWidth*maxWidth;
drawer.drawImage(img, 0, 0, canvas.width, canvas.height);
let base64 = canvas.toDataURL("image/jpeg", quality); // 压缩后的base64图片
let file = this.dataURLtoFile(base64,Date.parse(Date())+'.jpg');
file={content:base64,file:file};
console.log(file);//压缩后的file
this.onimg(file);
},
//base64转file
dataURLtoFile(dataurl,filename){
let arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr],filename,{type:mime});
},
//图片上传
onimg(file){
//console.log(file.content)
var formData = new FormData();
formData.append("file", file.file);
instance({
url:'/api/img/upload',
method:'post',
headers: {
'token': sessionStorage.token
},
data:formData
})
.then(response=>{
console.log(response)
this.imgmurl=response.data.url;
})
.catch(error=>{
console.log(error)
})
}, }
}
</script>
<style scoped>
.rwlctb_div{
padding: 10px 20px;
line-height: 26px;
} .tb_rwt span{
display: inline-block;
width: 100%;
}
.tb_rwt{
padding: 10px 20px;
}
.tb_rwt span{
display: inline-block;
width: 100%;
}
.tb_title{
text-align: center
}
.van-panel{
margin-top: 10px;
}
.van-row{
text-align: center
}
.van-uploader{
width:1.4rem;
border:1px solid #999;
text-align: center;
height: 1.4rem;
line-height: 1.4rem;
}
.img{
width:1.4rem;
border:1px solid #999;
text-align: center;
height: 1.4rem;
line-height: 1.4rem;
overflow: hidden;
position: relative;
}
.img img{
width: 100%;
}
.img i{
position: absolute;
top: 1px;
right: 1px;
z-index: 11;
}
</style>

vue里图片压缩上传组件的更多相关文章

  1. vue Base64图片压缩上传OSS

    this.compress(result, 800, 0.5).then(val => { //得到压缩图片 let data = val; that.file = that.dataURLto ...

  2. 基于vue + axios + lrz.js 微信端图片压缩上传

    业务场景 微信端项目是基于Vux + Axios构建的,关于图片上传的业务场景有以下几点需求: 1.单张图片上传(如个人头像,实名认证等业务) 2.多张图片上传(如某类工单记录) 3.上传图片时期望能 ...

  3. 三款不错的图片压缩上传插件(webuploader+localResizeIMG4+LUploader)

    涉及到网页图片的交互,少不了图片的压缩上传,相关的插件有很多,相信大家都有用过,这里我就推荐三款,至于好处就仁者见仁喽: 1.名气最高的WebUploader,由Baidu FEX 团队开发,以H5为 ...

  4. Html5+asp.net mvc 图片压缩上传

    在做图片上传时,大图片如果没有压缩直接上传时间会非常长,因为有的图片太大,传到服务器上再压缩太慢了,而且损耗流量. 思路是将图片抽样显示在canvas上,然后用通过canvas.toDataURL方法 ...

  5. 纯原生js移动端图片压缩上传插件

    前段时间,同事又来咨询一个问题了,说手机端动不动拍照就好几M高清大图,上传服务器太慢,问问我有没有可以压缩图片并上传的js插件,当然手头上没有,别慌,我去网上搜一搜. 结果呢,呵呵...诶~又全是基于 ...

  6. springMVC多图片压缩上传的实现

    首先需要在配置文件中添加配置: <!--配置文件的视图解析器,用于文件上传,其中ID是固定的:multipartResolver--> <bean id="multipar ...

  7. 基于H5+ API手机相册图片压缩上传

    // 母函数 function App(){} /** * 图片压缩,默认同比例压缩 * @param {Object} path * pc端传入的路径可以为相对路径,但是在移动端上必须传入的路径是照 ...

  8. 分享图片压缩上传demo,可以选择一张或多张图片也可以拍摄照片

    2016-08-05更新: 下方的代码是比较OLD的了,是通过js进行图片的剪切 旋转 再生成,效率较低. 后来又整合了一个利用native.js本地接口的压缩代码 ,链接在这 .页面中有详细的说明, ...

  9. js 图片压缩上传(base64位)以及上传类型分类

    一.input file上传类型 1.指明只需要图片 <input type="file" accept='image/*'> 2.指明需要多张图片 <input ...

随机推荐

  1. CI框架 session 不能读取的问题,PHP7环境

    根本原因在这,libraries/Session/Session.php 中 128行: 如果sessionid的长度不是40的话,每次执行都会 unset($_COOKIE[ci_session]) ...

  2. js 手机号加密 中间星号表示

    var tel = String(this.memberMsg.phoneNo); var dh=tel.substr(0,3)+"******"+tel.substr(8); r ...

  3. WhaleCTF之web-本地登录

    WhaleCTF之web-本地登录 前往题目 呵呵,基然对ip限制,没事,掏出burp抓包 改包 添加XFF头部 X-Forwarded-For: 127.0.0.1 发一下 不是admin,没有用户 ...

  4. 05_springmvc参数绑定

    一.参数绑定的过程 从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上.springmvc中,接收页面提交的数据是通过方法形参来接收.而不 ...

  5. WinForm中Dispose()和Close()的区别

    WinForm中Dispose()和Close()的区别 Close()会自动调用Dispose()方法,但是如果窗体是模态的,则不会调用 所以ShowDialog的时候,要用Dispose(),Sh ...

  6. java开发系列-Http协议

    概述 HTTP(HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.这种协议用来规定通信数据的格式. HTTP请求 浏览器往服务器发送数据称之为请求.HTTP ...

  7. [Swoole系列入门教程 1] CentOs 上的Swoole安装

    如果已经用宝塔安装好了PHP, 开启相应的扩展 一.编译安装git clone https://github.com/swoole/swoole-src.git cd swoole-src/ phpi ...

  8. mac上安装软件后,桌面上软件的图标如何去掉?

    桌面上的图标是软件的镜像包,默认安装以镜像形式,你选中它,按command+e 就可以推掉它

  9. hammer.js使用

    手势包括点击(tap),长按(press),滑动(swipe),方向(pan) 使用实例: <!DOCTYPE html> <html> <head> <me ...

  10. ArcMap应用——三维场景中井盖的属性配置

    在精细三维场景中,有地面(包括道路面.马路牙子).有部件数据(包括井盖).我们会发现有马路牙子的地方比道路面要高出一部分,比如0.1米,但是雨水井盖却有些在路面上.有些在道路以外.就是说在道路面上的井 ...