微信小程序> 微信小程序上传照片,限制格式,限制大小,公用方法

微信小程序上传照片,限制格式,限制大小,公用方法

浏览量:1266 时间: 来源:生生不息的希望

方法

/** * 选择上传照片 * page_that:调用方法的页面, * cb_key:返回参数数组的名称 * count:上传张数 * 返回值:数组 */function selecImg(page_that, cb_key, count) {  console.log("选择图片");  wx.chooseImage({    count: count,    sizeType: ['compressed'],    sourceType: ['album', 'camera'],    success: function(res) {      console.log(res)      var pathArray = [];      var PicNums = res.tempFiles.length;      for (var inta = 0; inta < PicNums; inta++) {        var size = res.tempFiles[inta].size;        var path = res.tempFiles[inta].path;        var formatImage = path.split(".")[(path.split(".")).length - 1];        console.log("图片格式" + formatImage)        if (formatImage != "png" && formatImage != "jpg" && formatImage != "jpeg") {          return wx.showToast({            title: '只能上传.png、.jpg、.jpep 格式',            icon: 'none',            image: '',            duration: 2000,            mask: true,          })        }        if (config.image_size < size) {          return wx.showToast({            title: '图片大小限制:' + (config.image_size / 1024 / 1024) + "MB",  ///config.image_size 配置文件中设置            icon: 'none',            image: '',            duration: 1500,            mask: true,          })        }        pathArray.push(path);        if (inta == PicNums-1) {          var cb = {};          cb[cb_key] = pathArray;          page_that.setData(cb);        }      }    }  })}
.wxml
<image src="../../Image/bg_pic1.png" mode='widthFix' style="width:100px" bindtap='Oncarm' data-whopic='idcard'></image>

.js 调用

  Oncarm: function(e) {    var whopic = e.currentTarget.dataset.whopic;    /**     * page_that:调用方法的页面,     * cb_key:返回参数数组的名称,     * count:上传张数,     * 返回值:数组     */    util.selecImg(this, whopic, 1);  },

小程序

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎