[微信小程序]上传单张和多张图片

上传单张图片并展示, 

<button bindtap="upimg" class='jia_img' >上传</button>
<image src="{{tempFilePaths[0]}}"></image>
data{
tempFilePaths:[];
},
  upimg: function () {
    wx.chooseImage({
      success: function (res) {
        var data = {
          program_id: app.jtappid
        }
        var tempFilePaths = res.tempFilePaths  //图片
        wx.uploadFile({
          url: 'aaa.cn', //仅为示例,非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'add_image', //文件对应的参数名字(key)
          formData: data,  //其它的表单信息
          success: function (res) {
          }
        })
      }
    })
  },

上传多张图片并展示:
<view class="big-logos">
  <image bindtap="upimg" src='../../../image/s.png'></image>
  <block wx:for="{{img_arr}}" wx:key="index">
    <view class='logoinfo'>
      <text class='xxx' bindtap='xxx' data-index='{{index}}'>x</text>
      <image src='{{item}}'></image>
    </view>
  </block>
</view>
<button class='top_20 btn' bindtap="upconfirm">确定</button>
  upconfirm: function () {
    this.up();
  },
  up: function () {
    var that = this;
    data = {
      openid: app.openid,
      program_id: app.program_id,
      only_num: only_num
    }
    wx.uploadFile({
      url: 'pg.php/Aishen/upload_photo',
      filePath: that.data.img_arr[i],
      name: 'image', //文件对应的参数名字(key)
      formData: data,  //其它的表单信息
      success: function (res) {
      }, complete: function (complete) {
        console.log(complete)
        i++
        if (i == that.data.img_arr.length) {
          util.request('https://sz800800.cn/pg.php/Aishen/uploade_photo_r', 'post', { 'only_num': only_num }, '正在加载数据', function (res) {
            console.log(res)
            if (res.data.state == 1) {
              wx.showModal({
                title: '提示',
                content: '提交成功!',
                success: function (res) {
                  that.onLoad()
                  wx.navigateBack({
                    delta: 1
                  })
                }
              })
            } else {
              wx.showModal({
                title: '提示',
                content: '提交失败,请重新提交!',
              })
            }
          })
        } else if (i < that.data.img_arr.length) {//若图片还没有传完,则继续调用函数
          that.up()
        }
      }
    })
  },

  // 上传
  upimg: function () {
    var that = this;
    if (this.data.img_arr.length < 12) {
      wx.chooseImage({
        sizeType: ['original', 'compressed'],
        success: function (res) {
          that.setData({
            img_arr: that.data.img_arr.concat(res.tempFilePaths)
          })
          num = that.data.img_arr.length
        }
      })
    } else {
      wx.showToast({
        title: '最多上传12张图片',
        icon: 'loading',
        duration: 3000
      });
    }
  },







猜你喜欢

转载自blog.csdn.net/qq_35713752/article/details/78276368