微信小程序怎样制作上传图片功能,并可多选、查看和删除
Admin 2022-11-19 群英技术资讯 858 次浏览
这篇文章主要介绍了“微信小程序怎样制作上传图片功能,并可多选、查看和删除”相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇微信小程序怎样制作上传图片功能,并可多选、查看和删除文章都会有所收获,下面我们一起来看看吧。具体内容如下
用到的api
wx.chooseMedia(); 用于拍摄或从手机相册中选择图片或视频
功能:点击上传图片,可多选,或者拍照上传;点击图片放大查看;长按图片删除
效果图

json里面引用weui的组件uploader
{
"navigationBarTitleText": "评价工单",
"usingComponents": {
"mp-uploader": "weui-miniprogram/uploader/uploader",
"mp-cells": "weui-miniprogram/cells/cells",
"mp-cell": "weui-miniprogram/cell/cell"
}
}
wxml
<view class="weui-uploader">
<view class="img-v weui-uploader__bd">
<view class='pic' wx:for="{{technicianAssessPicture}}" wx:for-item="item" wx:key="*this">
<image class='weui-uploader__img ' src="{{item}}" data-index="{{index}}" mode="aspectFill" bindlongpress="deleteTechnician" bindtap="previewTechnician">
</image>
</view>
<view class="weui-uploader__input-box pic" bindtap="technicianImg"> </view>
</view>
</view>
js
data:(){
technicianAssessPicture: [], // 技师图片
}
// 上传技师图片
technicianImg: function (e) {
var that = this;
var technicianAssessPicture = this.data.technicianAssessPicture;
if (technicianAssessPicture.length >= 9) {
this.setData({
lenMore: 1
});
setTimeout(function () {
that.setData({
lenMore: 0
});
}, 2500);
return false;
}
wx.chooseMedia({
count: 9, // 默认9
mediaType: ['image','video'], // 文件类型
// image 只能拍摄图片或从相册选择图片
// video 只能拍摄视频或从相册选择视频
// sizeType: ['original', 'compressed'], //所选的图片的尺寸 original原图,compressed压缩图
// 仅对 mediaType 为 image 时有效,是否压缩所选文件
sourceType: ['album', 'camera'], //图片和视频选择的来源
maxDuration: 30, // 拍摄视频最长拍摄时间,单位秒。时间范围为 3s 至 60s 之间。不限制相册。
camera: 'back', // 仅在 sourceType 为 camera 时生效,使用前置或后置摄像头
// back 使用后置摄像头;front 使用前置摄像头
success: function (res) {
var tempFilePaths = res.tempFiles;
var technicianAssessPicture = that.data.technicianAssessPicture;
for (var i = 0; i < tempFilePaths.length; i++) {
if (technicianAssessPicture.length >= 9) {
that.setData({
technicianAssessPicture: technicianAssessPicture
});
return false;
} else {
const tempFilePaths1 = tempFilePaths.map(v=>v.tempFilePath)
// tempFilePaths数据是json数组,我们需要的是普通数组需要处理一下
technicianAssessPicture.push(tempFilePaths1[i]);
}
}
that.setData({
technicianAssessPicture: technicianAssessPicture
});
console.log(that.data.technicianAssessPicture, 'hhhhhhhhhhhhhhhhhhhhh');
}
});
},
处理后打印出来的数据

预览,删除
// 预览图片
previewTechnician: function (e) {
//获取当前图片的下标
var index = e.currentTarget.dataset.index;
//所有图片
var technicianAssessPicture = this.data.technicianAssessPicture;
wx.previewImage({
//当前显示图片
current: technicianAssessPicture[index],
//所有图片
urls: technicianAssessPicture
})
},
// 长按删除
deleteTechnician: function (e) {
var that = this;
var technicianAssessPicture = that.data.technicianAssessPicture;
var index = e.currentTarget.dataset.index; // 获取当前长按图片下标
wx.showModal({
// cancelColor: 'cancelColor',
title: '提示',
content: '确定要删除此图片吗?',
success: function (res) {
if (res.confirm) {
console.log('确定');
technicianAssessPicture.splice(index, 1);
} else if (res.cancel) {
console.log('取消');
return false;
}
that.setData({
technicianAssessPicture
})
}
})
},到此,关于“微信小程序怎样制作上传图片功能,并可多选、查看和删除”的学习就结束了,希望能够解决大家的疑惑,另外大家动手实践也很重要,对大家加深理解和学习很有帮助。如果想要学习更多的相关知识,欢迎关注群英网络,小编每天都会给大家分享实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
JavaScript是一个动态类型语言,在运行时获取变量类型是常用操作。本文将通过示例为大家详细讲讲在JavaScript中如何判断变量类型,感兴趣的可以学习一下
JS数组中filter方法的使用是什么?filter()创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。
本文主要给大家解的是关于vue懒加载的三种方式,包括vue异步组件、ES6的import()和webpack的require.ensure(),下本有具体代码,具有一定的参加价值,大家可以参考。
本文主要介绍了Typescript中使用引用路径别名报错的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
JavaScript怎么创建多个对象?javascript面向对象创建多个对象的方法,哪个最好用?字面量?工厂模式方法?构造函数方法?原型方法?原型加构造函数方法?进来一看便知。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008