//单图上传
<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. import: not authorized `time' @ error/constitute.c/WriteImage/1028. import: not authorized `rospy' @ error/constitute.c/WriteImage/1028.

  2. springboot thymeleaf ----服务端渲染html

    一. 引用命名空间 <html xmlns:th="http://www.thymeleaf.org"> 不这么写 html标签没闭合会报错 二.实际内容在../sta ...

  3. 根据url提取网站域名的方法小结

    前言:最近使用到了他人总结的一个基础类库.查看了下源码,发现String帮助类的一个辅助方法不是很严谨,重构之. 1.原来程序的写法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  4. 事件处理器v-on:(event)/@(event)

    <!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...

  5. PAT甲级——A1073 Scientific Notation

    Scientific notation is the way that scientists easily handle very large numbers or very small number ...

  6. hibernate查询timestamp条件

    参考https://blog.csdn.net/zuozuoshenghen/article/details/50540661 Mysql中Timestamp字段的格式为yyyy-MM-dd HH-m ...

  7. js中读取json数据

    1.JSON字符串转为JSON对象 var obj = eval('('+data+')');① var obj = data.praseJSON();② var obj = JSON.prase(d ...

  8. Python自学--part2

    概要 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 一.列表.元祖操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 na ...

  9. 第一周课堂笔记3th

    1.bool值    True正确                False错误   数字为0时的显示  为空值时“” 2. str   int   bool布尔值  之间的转化 str   int  ...

  10. C++【vector】用法和例子

    /*** * vector 基础api复习 * 8 AUG 2018 */ #include <iostream> #include <vector> using namesp ...