微信小程序本地上传图片预览并删除

news/2024/7/20 2:48:58 标签: 小程序, 微信

微信小程序本地上传图片,在小程序开发文档上面有说明:

wx.chooseImage(Object object) | 微信开放文档

wx.previewImage(Object object) | 微信开放文档

 

 wxml: 图片用wx:for循环,后面用带有加号的图片。

<view class="content">
    <text>微信小程序练习demo</text>
    <view class="upload">
        <view class="imgArr" wx:if="{{upImage.length > 0}}" wx:for="{{upImage}}" wx:key="index">
            <image src="{{item}}" class="upImage" mode="widthFix" bindtap="previewImage" data-src="{{item}}"></image>
            <image class="delete" bindtap="delete" src="../../images/cha.png" data-id="{{index}}"></image>
        </view>
        <image wx:if="{{upImage.length < 9}}" src="../../images/shangc.png" class="upImage" bindtap="uploadImg"></image>
    </view>
</view>

wxss:

.content{
    width: 100%;
    padding: 0 20rpx;
}
.upload{
    margin-top: 20rpx;
    width: 100%;
    height: 200rpx;
    display: flex;
    flex-wrap: wrap;
}
.imgArr{
    display: flex;
    position: relative;
}
.imgArr:nth-child(3n+2){
    margin-bottom: 15rpx;
}
.imgArr .upImage{
    margin-right:15rpx;
}
.upImage{
    width: 200rpx;
    height: 200rpx;
}
/* .upImage:nth-child(2){
    margin-left: 15rpx;
} */
.delete{
    width: 40rpx;
    height: 40rpx;
    position: absolute;
    top: -5rpx;
    right: -5rpx;
}

js:

data: {
        upImage:[]
    },

//图片上传
    uploadImg(){
        let that = this;
        wx.chooseImage({
          count: 9,
          sizeType: ['original', 'compressed'],
          sourceType: ['album', 'camera'],
          success(res){
              console.log(res);
              const tempFilePaths  = res.tempFilePaths[0];
              let old_data = that.data.upImage;
              let new_data = res.tempFilePaths;
              if(that.data.upImage.length + res.tempFilePaths.length <=9){
                  that.setData({
                    upImage : old_data.concat(new_data)
                  })
                  console.log(that.data.upImage)
              }else{
                  wx.showToast({
                    title: '上传图片不能超过9张',
                    icon:"none"
                  })
              }
          }
        })
    },
    //图片预览
    previewImage(e){
        let that = this;
        let current = e.currentTarget.dataset.src;
        console.log(current);
        let urls = that.data.upImage;
        console.log(urls);
        // urls.push(current);
        wx.previewImage({
            current: current, // 当前显示图片的http链接
            urls: urls // 需要预览的图片http链接列表
        })
    },
    //图片删除
    delete(e){
        console.log(e);
        let that = this;
        let upImage = that.data.upImage;
        let index = e.currentTarget.dataset.id;
        console.log(index);
        wx.showModal({
          title:"提示",
          content:"确定要删除此图片吗?",
          success:function (res) {
              if(res.confirm){
                  console.log("点击了确定");
                  upImage.splice(index,1);
                  that.setData({
                    upImage
                  })
              }else if(res.cancel){
                console.log("点击了取消");
              }
          }
        })
    },

 最后的效果大概是:

 


http://www.niftyadmin.cn/n/1038990.html

相关文章

win8 php mysql_windows8.1下Apache+Php+MySQL配置步骤

软件版本&#xff1a;apache&#xff1a;Apache 2.4.6 Win64PHP&#xff1a;PHP 5.5 VC11 x64 Non Thread SafeMySql:5.5 win64Mysql安装&#xff1a;双击运行,设置用户名、密码、编码(utf8)。PHP配置&#xff1a;1、解压下载的zip文件到一个目录&#xff0c;本教程中为&#x…

微信小程序上拉加载

自己写demo,做一个微信小程序不通过后端传输数据本地循环数据上拉加载的效果。 效果是这样的&#xff0c;代码&#xff1a; wxml: <view class"contain"><view class"listCont"><view class"li" wx:for"{{arrayList}}&quo…

js常用的表单正则验证

var phoneReg /^1[3456789]\d{9}$/; //手机号码验证 var nameReg /^[\u4E00-\u9FA5]{2,4}$/; //中文名验证 var idReg /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; //身份证验证 let userName""; let phoneNum ""; let idCard ""; …

uniapp定时器定时刷新获取

要用uniapp做一个效果&#xff1a;页面定时刷新获取&#xff0c;根据时间变换状态。 这里按钮有5种状态&#xff1a;预约&#xff0c;已预约&#xff0c;已售罄&#xff0c;未开始&#xff0c;已结束。要把当前时间和后端返回的时间做对比&#xff0c;如果还没到开始时间则显示…

搭建 php+mysql+nginx_php+mysql+nginx+liunx 服务搭建

安装php7相应的yum源CentOS 7.x:# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpmCentOS 6.x:# rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm查看版本y…

记录一次用vant框架做h5微信公众号

这次是用vant框架做一个h5微信公众号&#xff0c;是在我一同事封装的基础上开发的。封装的项目在gitee上的地址&#xff1a;湖南巨仑网络科技有限公司/vant-H5移动端封装 - Gitee.com。内容有底部导航的&#xff0c;轮播的&#xff0c;时间转时间戳的&#xff0c;axios请求的一…

php如何实现从mysql查询功能_PHP实现MYSQL的查询功能

展示数据库header(Content-Type: text/html;charsetutf-8);$host "127.0.0.1";$port 3306;$user root;$pass 123654;$charset utf8; //设置默认字符$link mysql_connect("$host:$port",$user,$pass); //链接数据库$sql show databases; //展示数据s…

vue组件的使用及父子组件之间通信

之前了解到vue父子组件之间传值&#xff1a; 父组件传给子组件&#xff1a;通过在页面引用里 传值&#xff1a; 在父组件内引入子组件 <childCompont :parmas"parentParmas"></childCompont> 那么父组件的 parentParmas就会传入子组件内&#xff1b; 子…