收藏
回答

wx.uploadFile 上传多张图片

为什么我上传多经图片每次都只能上传成功一张图片而以啊!


 /**

   * 上传图片

   * filePaths:文件路径数组

   * i 文件路径数组坐标

   * length 文件路径数组的长度

   */

  uploadDIY: function (that,filePaths, i, length, circle_id){

    wx.uploadFile({

      url: app.globalData.webURL,

      filePath: filePaths[i],

      name: 'file',

      formData: {

        m: 'api',

        c: 'circle',

        a: 'upload',

        imgType: 'circle',

        circle_id: circle_id

      },

      success: function (res) {

        app.wx_error('上传完成第'+i+'张');

      },

      complete:function(res){

        i++;

        if (i == length) {

          app.wx_error('上传完成');

        }

        else {  //递归调用uploadDIY函数

          app.wx_error('i=' + i + " <= length =" + length);

          if (i <= length){

            app.wx_error('正要第' + i + "次调用uploadDIY");

            that.uploadDIY(that, filePaths, i, length, circle_id);

          }

        }

      }

    });

  },


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

5 个回答

  • 小糖糖
    小糖糖
    2017-10-13

    能同时上传九张,但是会覆盖之前上传的,也可以显示上传进度


    upload:function(){

      var that=this

      wx.chooseImage({

        count: 9, 

        sizeType: ['original', 'compressed'], 

        sourceType: ['album', 'camera'], 

        success: function (res) {

          var tempFilePaths = res.tempFilePaths


          console.log(tempFilePaths)

          wx.uploadFile({

            url: '

    https://example.weixin.qq.com/upload

    ', 

            filePath: tempFilePaths[0],

            name: 'file',

            formData:{

              'user': 'test'

            },

            success: function(res){

              var data = res.data

              wx.showModal({

                title: '上传文件返回状态',

                content: '成功',

                success: function(res) {

                  if (res.confirm) {

                    console.log('用户点击确定')

                  }

                }

              }) 

              const uploadTask = wx.uploadFile({

                url: '

    https://example.weixin.qq.com/upload

    ', //仅为示例,非真实的接口地址

                filePath: tempFilePaths[0],

                name: 'files',

                formData:{

                  'user': 'test'

                },

                success: function(res){

                  var data = res.data

                    //do something

                  }

                })


              uploadTask.onProgressUpdate((res) => {

                that.setData({

                  progress:res.progress

                })


                console.log('上传进度', res.progress)

                console.log('已经上传的数据长度', res.totalBytesSent)

                console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)

              })


            uploadTask.abort() // 取消上传任务                         

          },

          fail:function(res){

            console.log(res)

          }

        }),



          that.setData({

            path:tempFilePaths

          })  

        }

      })

    },


    2017-10-13
    有用
    回复
  • 跃在渊
    跃在渊
    2017-08-15

    求解啊,递归用了,循环也试了,Android就是不行啊

    2017-08-15
    有用
    回复
  • wind
    wind
    2017-05-20

    回复1楼,我用的是递归调用

    2017-05-20
    有用
    回复
  • 天蓝
    天蓝
    2017-05-20

    超链接怎么接入?



    2017-05-20
    有用
    回复
  • 只争朝夕
    只争朝夕
    2017-05-20

    循环没有

    2017-05-20
    有用
    回复
登录 后发表内容