【uniapp】微信小程序图片预览

一、效果图

在这里插入图片描述

二、代码块

<template>
	<view>
		<view v-for="(item,index) in photos" :key="index" >
			<view class="box">
				<image :src="item.pics_small_url" mode="widthFix" @click="previewImage(index)"></image>
			</view>
		</view>
</template>

<script>
	export default {
		data() {
			return {
				photos: [{
					id: '1',
					pics_small_url:'https://fc5tn.baidu.com/it/u=1112089068,583547811&fm=202&src=787&fc_m=cvican',
					pics_big_url:'https://fc5tn.baidu.com/it/u=1112089068,583547811&fm=202&src=787&fc_m=cvican'
				},{
					id: '2',
					pics_small_url:'https://fc5tn.baidu.com/it/u=1789275420,3071612989&fm=202&src=787&fc_m=cvican',
					pics_big_url:'https://fc5tn.baidu.com/it/u=1789275420,3071612989&fm=202&src=787&fc_m=cvican',
				}] // 存放从后台获取的数据
			}
		},
		methods:{
			previewImage(index) {
				let photoList = this.photos.map(item => {
					return item.pics_big_url;
				});
				uni.previewImage({
					current: index,
					urls: photoList
				});
			}
		}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值