小程序 多图上传到后台图片不一致

刚开始我写的 其他不用管 主要是看图片上传的问题
/* 上传图片 */
  chooseImage: function(e) {
    var that = this;
    wx.chooseImage({
      count: this.data.uploadTotal - this.data.picfiles.length, //最多6张
      //count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          picfiles: that.data.picfiles.concat(res.tempFilePaths)
        });
        //每次返回俩个对应的地址  sizeType 
        let length = res.tempFilePaths.length;
        for (var i = 0; i < length; i++) {
          var imgUrl = res.tempFilePaths[i];
          //上传服务器
          wx.uploadFile({
            url: app.globalData.Murl + '/index.php?controller=student_myself&action=upload_files',
            filePath: imgUrl,
            name: 'upload_files',
            header: {
              'content-type': 'multipart/form-data'
            },
            formData: {
              type: 's_head_ico'
            },
            success: function(res) {
              console.log('result', res)
              if (res.statusCode == 200) {
                that.data.head_img_url.push(JSON.parse(res.data).result.datas);
                console.log(that.data.head_img_url, that.data.picfiles[i])
                that.setData({
                  head_img_url: that.data.head_img_url
                })
              }
            },
            fail: function() {

            }
          })
        }

      }
    })
  },

后来发现 存到数据库的图片和上传的不一致,大概原因是 由于小程序的api写着每次只能传一张,所以我这里写的多图上传或导致 第一张图还没传过去还没成功第二张图又开始传了(尽量不用for循环,如果你有更好的请略过) ,这样下来 删俩张再添加俩张就会不一致了。主要 实现传完一张成功后再传一张用了一下简单递归, 后期修改如下:

/* 上传图片 */
  chooseImage: function(e) {
    var that = this;
    wx.chooseImage({
      count: this.data.uploadTotal - this.data.picfiles.length, //最多6张
      //count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          picfiles: that.data.picfiles.concat(res.tempFilePaths)
        });
        //每次返回俩个对应的地址  sizeType 
        let length = res.tempFilePaths.length;
        var i=0;
        imgUrl(i);
        function imgUrl(i){
          console.log(res.tempFilePaths[i])
          wx.uploadFile({
            url: app.globalData.Murl + '/index.php?controller=student_myself&action=upload_files',
            filePath: res.tempFilePaths[i],
            name: 'upload_files',
            header: {
              'content-type': 'multipart/form-data'
            },
            formData: {
              type: 's_head_ico'
            },
            success: function (res) {
              console.log('result', res)
              if (res.statusCode == 200) {
                that.data.head_img_url.push(JSON.parse(res.data).result.datas);
                console.log(that.data.head_img_url, that.data.picfiles[i])
                that.setData({
                  head_img_url: that.data.head_img_url
                })
              }
              i++;
              if (i >= length){
                return false;
              }else{
                imgUrl(i);
              }
            },
            fail: function () {

            }
          })
        }
      }
    })
  },
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无名小码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值