微信小程序多图上传

选择图片wx.chooseImage

这里写图片描述
这里写图片描述

上传图片wx.uploadFile

这里写图片描述

这里写图片描述


  1. 点击选择图片
chooseImg: function () {
    var that = this;
    var selectImg = that.data.selectImg;
    wx.chooseImage({
      count: 5,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {

        var tempFilePaths = res.tempFilePaths

        if (tempFilePaths.length + selectImg.length > 5) {
          that.tips('最多只能上传5张')
        } else {
          for (var i = 0; i < tempFilePaths.length; i++) {
            selectImg.push(tempFilePaths[i])
            // that.uploadImg(tempFilePaths[i])
          }
          that.setData({
            selectImg: selectImg
          })
          console.log(selectImg);
        }

      },
      fail: function () {
        that.tips('选取图片失败')
      }
    })
  },
  1. 提交表单,遍历selectImg ,调用uploadImg()进行图片一张张上传。
  2. 上传图片
uploadImg: function (img,fn){
    var that = this;
    var imgs = that.data.imgs;
    app.getLoginSession(function (session) {
      wx.uploadFile({
        url: app.api,
        filePath: img,
        name: 'file',
        formData: {
          app: 'farm',
          act:'upload_file',
          filePath: img,
          t_session: session
        },
        method: 'POST',
        header: {
          'content-type': 'multipart/form-data'
        },
        success: function (res) {
          var data = JSON.parse(res.data);
          var rlt = data.retval;
          if (data.done) {

            imgs.push(rlt.save_path);
            that.setData({
              imgs:imgs
            })
            console.log("sss"+that.data.imgs)
            typeof fn == 'function' && fn();
          } else {
            console.log(data);
            that.tips('图片上传失败')
          }
        },
        fail:function(){
          console.log("失败");
        }
      })
    })
  },
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值