订单评价图片压缩 上传

做电商的时候,难免会有订单评价功能,怎样实现多图片的压缩并实现上传。类似这种评价

 

 

直接上完整的代码吧。可以慢慢分析,其实也很简单

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
		<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
		<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vant@1.6/lib/index.css">
		<script src="https://cdn.jsdelivr.net/npm/vant@1.6/lib/vant.min.js"></script>
		<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
		<title>订单评价</title>
	</head>
	<style type="text/css">
		.assess_starts{
			display: flex;
			flex-direction: row;
			align-items: center;
			padding: 15px;
			border-bottom: solid 0.5px #EBEDF0;
		}
		#start_img{
			width: 50px;
			height: 50px;
			object-fit: cover;
		}
		#span{
			margin-left: 8px;
			margin-right: 8px;
			font-size: inherit;
		}
		.van-rate{
			margin-right: 8px;
			margin-top: 5px;
		}
		#start_rate{
			color: rgba(0,0,0,0.5);
		}
		.assess_content{
			padding-bottom: 20px;
			border-bottom: solid 0.5px #ebedf0;
		}
		.textarea{
			width: 100%;
			height: 100%;
			border: none;
			padding: 5px 0 0 0;
			
		}
		.assess_photo{
			width: 80px;
			height: 80px;
			border: dashed 1px #cdcdcd;
			border-radius: 10px;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			margin-top: 15px;
		}
		.assess_photo img{
			
			width: 30px;
			height: auto;
		}
		#photo_span{
			color: #CDCDCD;
			margin-top: 10px;
		}
		.assess_button{
			display: flex;
			align-items: center;
			justify-content: center;
			margin-top: 70px;
		}
		.button{
			text-align: center;
			line-height: 40px;
			width: 80%;
			height: 40px;
			color: #FFFFFF;
			font-size: 1.3rem;
			letter-spacing: 5px;
			background-color: #27A3DF;
			border-radius: 10px;
		}
		.button:active{
			background-color:rgba(39, 163, 223 ,0.8) ;
		}
		.van-cell:not(:last-child)::after{
			border-bottom: none;
		}
		.add_photo{
			padding-left: 15px;
			padding-right: 15px;
			display: flex;
			flex-wrap: wrap;
			align-items: center;
		}
		#preview{
			width: 80px;
			height: 80px;
			object-fit: cover;
		}
		.show_photo{
			margin-right: 15px;
			position: relative;
			margin-top: 15px;
		}
		#remove{
			position: absolute;
			top: -10px;
			right: -10px;
			width: 20px;
			height: 20px;
		}
	</style>
	<body>
		<div class="orderAssessment">
			<div class="assess_starts">
				<img :src="src" id="start_img"/>
				<span id="span">描叙相符</span>
				<van-rate
				  v-model="value"
				  :size="24"
				  color="#f44"
				  void-icon="star"
				  void-color="#eee"
				></van-rate>
				<span id="start_rate">
					{{rate}}
				</span>
			</div>
			<div class="assess_content">
				<van-field
				    v-model="message"
				    maxlength="500"
				    type="textarea"
				    :placeholder="placeholder"
				    rows="1"
				    autosize
				></van-field>
				<div class="add_photo">
					<div class="show_photo" v-for="(item,index) in imgUrls" v-show="imgUrls.length>0">
						<img :src="item"  id="preview" @click="bigImg(index)"/>
						<img src="img/remove.png" @click="remove(index)" id="remove"/>
					</div>
					<!--//点击放大组件-->
					<van-image-preview
					  v-model="show"
					  :images="imgUrls"
					  :start-position="startIndex"
					  @change="onChange"
					>
					</van-image-preview>
					<!--//上传文件-->
					<div class="assess_photo" @click="getPhoto" v-show="imgUrls.length<6">
						<input type="file" id="upfile"  accept="image/*" multiple style="display: none;" @change="changeImage($event)"/>
						<img src="img/addPhoto.png"/>
						<span id="photo_span">
							{{addPhoto}}
						</span>
					</div>
				</div>
			</div>
			
			<div class="assess_button">
				<div class="button" @click="button">提交评价</div>
			</div>
		</div>
	</body>
	<script type="text/javascript">
		
		new Vue({
			el:'.orderAssessment',
			data:{
				src:"img/c.jpg",
				value:5,
				rate:"非常好",
				placeholder:"宝贝满足你的期待吗?说说你的使用心得,分享给想买的他们吧",
				message:"",
				addPhoto:"添加照片",
				photos:[],
				imgUrls:[],
				maxImg:6,
				leftImgs:0,
				show:false,
				startIndex:0
			},
			watch:{
				value(){
					if (this.value == 5) {
						this.rate = "非常好"
					}else if (this.value == 4) {
						this.rate = "好"
					}else if (this.value == 3) {
						this.rate = "一般"
					}else if (this.value == 2) {
						this.rate = "差"
					}else if (this.value == 1) {
						this.rate = "非常差"
					}
				}
			},
			methods:{
				getPhoto(){
					
					document.getElementById("upfile").click();
				},
				changeImage(e) {
			      	let file = e.target.files;
			      	console.log(file)
			      	let _this= this;
			      	if (file.length<=(_this.maxImg - _this.imgUrls.length)) {
			      		for (let i = 0;i<file.length;i++) {
			      			console.log(file[i]);
			      			var reader = new FileReader();
			      			reader.readAsDataURL(file[i]);
		      				reader.onload=function(e){
		      					var img = new Image();
								img.src = e.target.result;
			      				img.onload = function(){
			      					var canvas = document.createElement('canvas');
            						var context = canvas.getContext('2d');
									// 图片原始尺寸
					                var originWidth = this.width;
					                var originHeight = this.height;
					                if (originHeight*originWidth >1000000) {
					                	let ratio = originHeight*originWidth/1000000;
					                	//目标尺寸
					                	
					                	console.log("尺寸",ratio);
					                	let num = Math.sqrt(ratio);
					                	console.log(num);
					                	var targetWidth = originWidth/num;
					                	var targetHeight = originHeight/num;
					                } else{
					                	var targetWidth = originWidth;
					                	var targetHeight = originHeight;
					                }
					                canvas.width = targetWidth;
					                canvas.height = targetHeight;
					                // 清除画布
					                context.clearRect(0, 0, targetWidth, targetHeight);
					                // 图片压缩
					                context.drawImage(img, 0, 0, targetWidth, targetHeight);
					                /*第一个参数是创建的img对象;第二三个参数是左上角坐标,后面两个是画布区域宽高*/
									
									var newUrl = canvas.toDataURL('image/jpeg', 0.92);
					                console.log(newUrl);
			      					_this.imgUrls.push(newUrl);
//			      					console.log(a.img.height);
			      				}

		      				}

			      		}
			      		//剩余张数
			      		_this.leftImgs = _this.maxImg -(file.length+_this.imgUrls.length);
			      		_this.addPhoto = _this.maxImg -(file.length+_this.imgUrls.length) +"/"+ _this.maxImg;
			      		
			      	} else{
			      		this.$toast('只能选择' + (this.maxImg - this.imgUrls.length) + '张了')
			      	}
			    },
			    remove(index){
			    	this.imgUrls.splice(index, 1);
			    	this.leftImgs ++;
			    	if (this.leftImgs == this.maxImg) {
			    		this.addPhoto = "添加照片"
			    	} else{
			    		this.addPhoto = this.maxImg -this.imgUrls.length +"/"+ this.maxImg;
			    	}
			    },
			    bigImg(index){
			    	this.startIndex = index;
			    	this.show = true;
			    },
			    onChange(index){
			    	this.startIndex = index;
			    },
			    button(){
			    	
			    	for (let i=0;i<this.imgUrls.length;i++ ) {
			    		console.log(this.imgUrls[i]);
//			    		const formData = new FormData();
//						formData.append('file',this.imgUrls[i]);
			    		console.log(formData);
			    		axios.post("你的上传路径",{
			    			file:this.imgUrls[i]
			    		}).then(data=>{
			    			console.log(data)
			    		}).catch(err=>{
			    			console.log(err)
			    		})
			    		
			    	}
			    }
			    
			}
		})
	</script>
</html>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中实现图片压缩上传的方法如下: 1.引入插件:首先需要引入一个图片压缩上传的插件,例如`vue-image-compressor`。 2.安装插件:使用npm或yarn安装插件,命令如下: ``` npm install vue-image-compressor ``` 或 ``` yarn add vue-image-compressor ``` 3.导入插件:在需要使用的组件中导入插件,并注册到Vue实例中,示例代码如下: ```javascript import Vue from 'vue' import VueImageCompressor from 'vue-image-compressor' Vue.use(VueImageCompressor) ``` 4.编写模板代码:在组件模板中添加一个文件上传按钮,并绑定一个`@change`事件来处理文件选择,示例代码如下: ```html <template> <div> <input type="file" @change="handleFileUpload" /> </div> </template> ``` 5.编写处理方法:在组件的`methods`选项中编写处理文件上传的方法,首先获取选择的文件,然后使用插件提供的压缩方法进行压缩,最后使用压缩后的图片进行上传,示例代码如下: ```javascript methods: { handleFileUpload(event) { const file = event.target.files[0] this.$compressImage(file, { quality: 0.6, // 压缩质量,范围为0到1(可选,默认为0.6) maxWidth: 800, // 最大宽度(可选,默认为800) maxHeight: 800 // 最大高度(可选,默认为800) }) .then(compressedImage => { // 进行图片上传逻辑 }) .catch(error => { // 处理压缩失败的情况 }) } } ``` 在上述代码中,通过`this.$compressImage`来调用插件提供的压缩方法,传入的参数为选择的文件和一些可选的压缩参数。压缩成功后,会返回一个压缩后的图片,可以将其直接上传到服务器,或者进行其他的操作。 通过以上步骤,在Vue中实现了图片压缩上传的功能。注意,使用插件的具体使用方法可能会根据插件的不同而有所不同,请根据实际情况进行相应的调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值