uni-app上传图片组件()

这是一个基于Vue的图片上传组件示例,展示了如何利用uni-app的API选择并上传图片,支持预览、删除图片功能,并将上传成功的图片URL传递给父组件。组件还包含了样式布局,确保图片显示和删除图标的位置正确。
摘要由CSDN通过智能技术生成
<template>
	<view>
		<view class="photo_b">
			<view class="shang_base">
				<view class="photoImg" :style="{width:imgWidth+'rpx',height:imgHeight+'rpx'}"
					v-for="(item,index) in list" :key='index'>
					<image :src="item" mode="" style="width: 100%;height: 100%;" />
					<image class="code_close" @click.stop="bindDelete(index)" :src="imageUrl + 'ic_pic_del@2x.png'" />
				</view>
				<view class="photoImg" v-if="list.length < imgNum">
					<image :style="{width:imgWidth+'rpx',height:imgHeight+'rpx'}" :src="imageUrl + imgBaseImage"
						mode="" @click="uploadImg"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import app from '../App.vue'
	export default {
		name: "uploadImg",
		data() {
			return {
				imageUrl: app.globalData.imageUrl,
				list: [],
				url: []
			};
		},
		props: {
			// 上传图片张数
			imgNum: {
				type: Number
			},
			// 图片宽度
			imgWidth: {
				type: Number
			},
			// 图片高度
			imgHeight: {
				type: Number
			},
			// 基础图
			imgBaseImage:{
				type:String
			}
		},
		methods: {
			// 上传
			uploadImg() {
				const that = this;
				uni.chooseImage({
					count: 1, //一次性上传张数
					sizeType: ['compressed'],
					success: function(res) {
						const tempFilePaths = res.tempFilePaths;
						uni.showLoading({
							title: "上传中.."
						})
						for (let i = 0; i < tempFilePaths.length; i++) {
							uni.uploadFile({
								url: 'http://xxxxxxxxx', //上传地址
								filePath: tempFilePaths[i],
								name: 'file',
								header: {
									'token': uni.getStorageSync('token')
								},
								formData: {},
								success: (res) => {
									uni.hideLoading()
									console.log(JSON.parse(res.data));
									const imgUrl = JSON.parse(res.data).data.fullurl;
									const imgUrls = JSON.parse(res.data).data.url;
									// let arr = []
									// arr.push(imgUrls)
									that.list.push(imgUrl)
									that.url.push(imgUrls)
									// that.urls2 = arr.join(",")
								}
							})
						}
						that.$emit('getImages', that.list)
					}
				})
			},
			// 删除图片
			bindDelete(i) {
				const that = this;
				that.list.splice(i, 1)
			},
		}
	}
</script>

<style>
	.photo_b {
		width: 100%;
		display: flex;
		align-items: center;
	}

	.shang_base {
		margin-top: 24rpx;
		display: flex;
		flex-wrap: wrap;
		align-items: center;
	}

	.photoImg {
		position: relative;
		margin-right: 10rpx;
		margin-bottom: 20rpx;
	}

	.photoImg:nth-child(4n) {
		margin-right: 0;
	}

	.code_close {
		position: absolute;
		right: 0;
		top: 0;
		width: 42rpx !important;
		height: 42rpx !important;
	}
</style>

使用:

<uploadImage :imgHeight="212" :imgWidth="336" :imgNum="1" :imgBaseImage='imgBaseImage' @getImages="getImages"></uploadImage>

methods:{
    getImages(list){
		console.log(list);
    },
}

    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值