uniapp上传图片及删除功能(附代码,拿来就用)

这是一个关于商品图片上传组件的代码实现,包括限制图片数量、大小、类型,并提供了删除图片的功能。用户可以选择从相册中选取图片,上传后显示在列表中,超过2M的图片将无法上传。此外,还实现了点击图片删除的确认提示功能。
摘要由CSDN通过智能技术生成

<template>
	<view class="column">
		<view class="moduleBox">
			<view class="title">商品图片</view>
			<view class="font22" style="margin: 30upx 0;color: #999;">上传图片大小不允许超过2M!</view>
			<view class="flex-wrap">
				<view class="imgwrap" v-for="(item, index) in upImgGroup">
					<!-- 删除小图标 -->
					<view class="iconfont icon-guanbi1" @click="closeImg(index)"></view>
					<image :src="item" style="width: 100%;height: 100%;"></image>
				</view>
				<!-- 自己找个图片吧 -->
				<image src="../../static/upimg.png" style="width: 120upx;height: auto;" mode="widthFix"
					@click="upimage"></image>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				upImgGroup: [], //上传图片集合
			};
		},
		methods: {
			// 上传图片
			upimage() {
				var _that = this;
				uni.chooseImage({
					count: 9, //上传图片的数量,默认是9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
						for (var i = 0; i < res.tempFilePaths.length; i++) {
							_that.upImgGroup.push(res.tempFilePaths[i])
						}
						_that.orderKey = _that.orderKey + 1;
						var tempFilePaths = res.tempFilePaths;
						var tempFilesSize = res.tempFiles[0].size; //获取图片的大小,单位B
						if (tempFilesSize >= 2 * 1024 * 1024) {
							this.$api.msg("上传图片大小不允许超过2M!");
							return;
						}
					}
				});
			},
			// 删除图片
			closeImg(index) {
				var that = this;
				uni.showModal({
					title: '提示',
					content: '是否删除当前图片',
					success: function(res) {
						if (res.confirm) {
							that.upImgGroup.splice(index, 1);
							let id = that.upImgList[index].consignmentGoodsImgId;
							that.deleteImgTwo(id);
							that.upImgList.splice(index, 1);
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
		},
	}
</script>

<style lang="scss">
	.moduleBox {
		width: 100%;
		background-color: #fff;
		padding: 30upx;
		margin-bottom: 20upx;
		font-size: 26upx;

		.title {
			font-weight: 600;
			font-size: 30upx;
		}

		.flex-wrap {
			width: 100%;
			display: flex;
			flex-wrap: wrap;
			align-items: center;
		}

		.imgwrap {
			position: relative;
			width: 120upx;
			height: 120upx;
			margin-right: 18upx;
			margin-bottom: 20upx;

			.iconfont {
				color: #eee;
				position: absolute;
				top: -10upx;
				right: -10upx;
				z-index: 1;
			}
		}
	}
</style>

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值