uniapp微信小程序上传多张照片,每张图片不得大于1M

<template>
	<view class="user">
		<button @click="chooseImage">上传图片</button>
		<button @click="upload">提交</button>
		<image v-for="(item,index) in imgSrc" :key="index" :src="item" mode=""></image>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				imgSrc:[],
			}
		},
		methods: {
			chooseImage() {
				if(this.imgSrc.length >= 3){
					uni.showToast({
						title:'上传图片数量已大于3张'
					})
					return
				}
				var _this = this
				uni.chooseImage({
					success(res) {
						console.log(res)
						if(res.tempFilePaths.length>3){
							uni.showToast({
								title:'图片数量不能大于3张',
								icon:'none'
							})
							return
						}
						var item = res.tempFiles.find(item=>{
							return item.size > 1024*1024
						})
						if(item){
							uni.showToast({
								title:'单张图片大小不得高于1M',
								icon:"none"
							})
                               return
						}
						_this.imgSrc.push(...res.tempFilePaths)
						
					}
				})
			},
			upload(){
				this.imgSrc.forEach(item=>{  //因为有3张图片,所以上传时循环上传
					uni.uploadFile({
						url:'',//这里写自己的接口
						filePath:item, 
						name:'file',
						success(res){
							 console.log(res)
						}
					})
				})
			}
		}
	}
</script>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Uniapp 中实现微信小程序多张图片上传,可以按照以下步骤进行操作: 1. 在页面中创建一个按钮,用于触发选择图片的操作。 2. 在按钮的点击事件中,调用 `uni.chooseImage` 方法来选择图片。这个方法会返回选择的图片的临时文件路径。 3. 将选择的图片路径保存在一个数组中。 4. 在页面中展示已选择的图片,可以使用 `v-for` 指令来遍历已选择的图片数组,使用 `uni.previewImage` 方法来预览图片。 5. 创建一个提交按钮,在点击事件中调用 `uni.uploadFile` 方法,将选择的图片上传到服务器。 下面是一个示例代码: ```html <template> <view> <button @tap="chooseImage">选择图片</button> <view v-for="(image, index) in imageList" :key="index"> <image :src="image" mode="aspectFit" @tap="previewImage(index)" /> </view> <button @tap="uploadImages">上传图片</button> </view> </template> <script> export default { data() { return { imageList: [], // 存放已选择的图片路径 }; }, methods: { chooseImage() { uni.chooseImage({ count: 9, // 最多可以选择的图片张数 success: (res) => { // 将选择的图片路径保存到 imageList 数组中 this.imageList = res.tempFilePaths; }, }); }, previewImage(index) { uni.previewImage({ urls: this.imageList, // 需要预览的图片路径列表 current: this.imageList[index], // 当前显示的图片链接 }); }, uploadImages() { // 遍历已选择的图片路径,逐个上传 this.imageList.forEach((image) => { uni.uploadFile({ url: 'https://your-upload-api-url', // 上传图片的接口地址 filePath: image, name: 'file', // 上传文件对应的 key 值 formData: {}, // 其他额外的参数 success: (res) => { console.log(res.data); }, }); }); }, }, }; </script> ``` 请注意替换示例代码中的上传接口地址为你自己的接口地址。另外,需要在 `manifest.json` 文件中添加相应的权限配置,以允许选择图片上传文件。 希望这个示例对你有帮助!如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值