uniapp 图片点击预览&删除&上传单张/多张图片

1.单张图片预览

<image :src="userInfoBbusiness"  @click="previewImg(userInfoBbusiness)"></image>

methods: {
	//单张图片预览
	previewImg(imgurl) {
		let _this = this;
		let imgsArray = [];
		imgsArray[0] = imgurl
		uni.previewImage({
			current: 0,
			urls: imgsArray
		});
	},
}

2.多张图片预览(并且可以上传、删除)

	<view class='image_list '>
		<!--1.显示图片-->
		<view class="item" v-for="(item,index) in imageList" :key="index">
			<image :src="host+item" mode="widthFix"></image>
			<view class="del" @click="delFun(index)">删除</view>
		</view>
		
		<!--2.上传图片(可多张--这里限制上传3张)-->
		<view class="image_box" v-if="imageList.length<3" @click="uploadFun('imageList')">
			<view class="image_box_item">
				<image src="../../static/images/img_sct@2x.png" mode="heightFix"></image>
				<view>点击上传</view>
			</view>
		</view>
	</view>
	
	<!--3.上传图片(单张头像)-->
	<view class="upload_con" @click="uploadFun('avatar')">
		<view class="upload_view">
			<image :src="avatar ? host+avatar:'../../static/images/icon_txsc@2x.png'" mode="aspectFit" class="upLoadImg"></image>
			<view class="upload_text">点此上传</view>
		</view>
	</view>

	data() {
		return {
			host: this.$api.hostImages,  
			imageList:[],
		}
	},
	methods: {
		 // 1.多张图片预览
		 previewImg(index) {
		 	let that= this;
		    let imgsArray = [];
		    for (let i = 0; i < that.imageList.length; i++) {
		         if (that.imgUrlList[i].videoUrl == "") {
		             imgsArray.push(this.$api.hostImages+that.imageList[i].imgUrl);
		          }
		    }
		     // #ifdef MP
		      uni.previewImage({
		          current: index - 1,
		          urls: imgsArray,
		       });
		     // #endif
		
		     // #ifndef MP
		        uni.previewImage({
		           current: index - 1,
		           urls: imgsArray,
		       	});
		      // #endif
		 },
		 
		 // 1.删除图片
		delFun(index) {
			console.log("删除", index)
			this.imageList.splice(index, 1)
		},
		
		//2.3.上传图片
		uploadFun(type) {
			let that = this
			uni.chooseImage({
				count: 3, //默认9
				sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
				sourceType: ['album'], //从相册选择
				success: function(res) {
					console.log("JSON", res);
					const tempFilePaths = [];
					res.tempFilePaths.forEach((item) => {
						uni.uploadFile({
							url: that.$api.host + that.$url.url.upload, //仅为示例,非真实的接口地址
							filePath: item,
							name: 'file',
							header: {
								token: uni.getStorageSync('token')
							},
							success: (uploadFileRes) => {
								console.log('uploadFileRes', JSON.parse(uploadFileRes.data).initialPreview[0]);
								if (type == 'imageList') {
									//多张图片
									that.imageList.push(JSON.parse(uploadFileRes.data)
										.initialPreview[0]);
								} else {
									//单张头像
									that.avatar = JSON.parse(uploadFileRes.data)
										.initialPreview[0]; //这里获取了单张的图片路径
									// that.person_image = JSON.parse(uploadFileRes.data);
								}

								// that.fileList.push(JSON.parse(uploadFileRes.data).data);
								console.log("that.fileList==>", that.fileList)
							}
						});
					})
				},
				fail: function(err) {
					console.log(err)
				}
			});
		},
		//上传结束
	}


	<style>
		.image_list .item{
			position: relative;
		}
		.image_list .del{
			width: 80rpx;
			height: 50rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			position: absolute;
			top: 30rpx;
			right: 30rpx;
			background-color: rgba(0, 0, 0, 0.4);
			color: #FFF;
			border-radius: 12rpx;
			font-size: 24rpx;
		}
	</style>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值