//单图上传
<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. python2 和Python3 转unicode占位。

  2. webstorm中使用git管理服务器上的代码——入门级

    一.首先要确保电脑已经成功安装好git了.(记住git的安装位置) 二.这里需要给webstorm配置一下:依次点击:file –> Settings –> Version Control ...

  3. 杂项-SpringBoot-Jasypt:Jasypt(安全框架)

    ylbtech-杂项-SpringBoot-Jasypt:Jasypt(安全框架) 1. 使用jasypt加密Spring Boot应用中的敏感配置返回顶部 1. 本文讲述了在Spring Boot/ ...

  4. 2019暑训第一场训练赛 |(2016-icpc区域赛)部分题解

    // 今天下午比赛自闭了,晚上补了题,把AC的部分水题整理一下,记录坑点并吸取教训. // CF补题链接:http://codeforces.com/gym/101291 A - Alphabet 题 ...

  5. 最新二进制安装部署kubernetes1.15.6集群---超详细教程

    00.组件版本和配置策略 00-01.组件版本 Kubernetes 1.15.6 Docker docker-ce-18.06.1.ce-3.el7 Etcd v3.3.13 Flanneld v0 ...

  6. spark生态圈简介

    原文引自:http://www.cnblogs.com/shishanyuan/p/4700615.html 1.简介 1.1 Spark简介 Spark是加州大学伯克利分校AMP实验室(Algori ...

  7. CF629E Famil Door and Roads【树上计数+分类讨论】

    Online Judge:Codeforces629E,Luogu-CF629E Label:树上计数,分类讨论,换根 题目描述 给出一棵n个节点的树.有m个询问,每一个询问包含两个数a.b,我们可以 ...

  8. C/C++常考基础面试题(更新)

    题目来自牛客网 解析部分来自牛客网 https://www.nowcoder.com/4685265 一 分析下面代码有什么问题? void test1() { char string[10]; ch ...

  9. SpringBoot Controller接收参数的几种方式盘点

    本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:SpringBoot Controller接收参数的几种方式盘点: SpringBoot Controller接收参数的几种常用方式盘点 ...

  10. 从github下载项目出现yes/no的选项,无法下载项目

    解决办法: # 本地执行: ssh-keygen # 将id_rsa_pub文件中公钥拷贝到github上的ssh认证 oodful@:~/Volumes/Term2 :::$cat ~/.ssh/i ...