收藏
回答

如果多张图片上传又不想递归可以解决吗

框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
小程序 Bug wx.uploadFile 工具 6.5.3 2.0.0

https://developers.weixin.qq.com/miniprogram/dev/api/network/upload/wx.uploadFile.html

如果多张图片上传又不想递归可以解决吗

回答关注问题邀请回答
收藏

6 个回答

  • 猛男陈阔
    猛男陈阔
    2020-06-03

    oss for循环可以的

    2020-06-03
    有用 1
    回复 5
    • 贾永
      贾永
      2020-06-03
      我写了个递归。但是掉接口好阿卡。
      2020-06-03
      回复
    • 猛男陈阔
      猛男陈阔
      2020-06-03回复贾永
      代码贴出来看看
      2020-06-03
      1
      回复
    • 贾永
      贾永
      2020-06-04
      uploadOneByOne(imgPaths,successUp, failUp, count, length,url){
          var that = this;
          wx.uploadFile({
            url: url, //仅为示例,非真实的接口地址
            filePath: imgPaths[count],
            name: 'file',//示例,使用顺序给文件命名
            header: {
            "sid": wx.getStorageSync('sid') || "",
            "oid": wx.getStorageSync('oid') || "",
            },
            formData: {
              'user': 'test'
            },
            success (res){
              console.log(res)
              console.log(res.data)
              if(JSON.parse(res.data).code==200){
                var attachment = that.data.attachment
                console.log(JSON.parse(res.data).data.path)
                attachment.push(JSON.parse(res.data).data.path[0])
                that.setData({
                  attachment:attachment
                })
                console.log(that.data.files)
                console.log(that.data.files.concat(imgPaths))
                that.setData({
                  files: that.data.files.concat([imgPaths[count]])
                });
              }else{
                wx.showToast({
                  title: JSON.parse(res.data).msg,
                  icon:'none',
                  duration:2000
                })
              }
              //do something
            },
            fail:function(e){
              failUp++;//失败+1
            },
            complete:function(e){
              count++;//下一张
              if(count == length){
                //上传完毕,作一下提示
                console.log('上传成功' + successUp + ',' + '失败' + failUp);
                wx.showToast({
                  title: '上传成功' + successUp,
                  icon: 'success',
                  duration: 2000
                })
              }else{
                //递归调用,上传下一张
                that.uploadOneByOne(imgPaths, successUp, failUp, count, length,url);
                console.log('正在上传第' + count + '张');
              }
            }
          })
        },
      2020-06-04
      回复
    • 贾永
      贾永
      2020-06-04回复猛男陈阔
      现在我们的方案就是。直接一次限制上传一张
      2020-06-04
      回复
    • 猛男陈阔
      猛男陈阔
      2020-06-05回复贾永
      不会卡的吧?setData渲染页面是异步的呀
      2020-06-05
      1
      回复
  • 侯建新
    侯建新
    2020-06-03

    我用的for循环,success里面用下标标识对应的数据

    2020-06-03
    有用
    回复
  • 四哥派
    四哥派
    2020-06-03

    谢邀,promise吧

    2020-06-03
    有用
    回复 1
    • 贾永
      贾永
      2020-06-03
      我写了个递归。但是调接口好阿。只能这样了吧。
      2020-06-03
      回复
  • 郑钱花
    郑钱花
    2020-06-03

    promise最好用

    2020-06-03
    有用
    回复
  • 卢霄霄
    卢霄霄
    2020-06-03

    for循环,记住对应下标,弄一堆promise,Promise.all

    2020-06-03
    有用
    回复
  • 浮小云
    浮小云
    2020-06-03

    没有,只有单图上传

    若认为该回答有用,给回答者点个[ 有用 ],让答案帮助更多的人

    2020-06-03
    有用
    回复
登录 后发表内容
问题标签