1. 程式人生 > >vue裏圖片壓縮上傳組件

vue裏圖片壓縮上傳組件

UNC cep accept posit click toa dcl v-on ati

//單圖上傳
<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裏圖片壓縮上傳組件