html52.com
我爱小程序-开发者交流社区

wx.uploadfile 使用for循环发生异步提交怎么解决?

var zhi = that.data.inputlist.length - 1;

for(var j=0; j<zhi; j++){

debugger

var pics = that.data.inputlist[j].img;

wx.uploadFile({

url: bbsUrl + '?op=-1&mid=' + wx.getStorageSync('mid'),

filePath: pics[j],

name: 'fileData',

dataType: 'json',

formData: null,

method: 'POST',

success: function (resp) {

console.log("1234567897899" + resp);

}

});

}


网友回复

? ? ? ? ?ʷᵃⁿᵍ:

如果非得要按顺序传 promise await async了解下,编辑器增强编译打开,就可以支持这些关键字;

一般批量上传是允许异步的,这样更快,上传时做个标识(j),上传完成根据标识放入数组

烬:

用递归

马乾:

promise了解一下

o0o往后余生:

递归上传

imgPaths 待上传的所有图片路径数组

currentIndx 当前要上传的图片数组下标,从0开始


oneUpload: function(imgPaths, currentIndx){

    var t = this

    console.log('正在上传第' + (currentIndx + 1) + '张图片')

    wx.uploadFile({

        //...

        filePath: imgPaths[currentIndx],

        success: function (res) {

            console.log('第' + (currentIndx + 1) + '张图片上传成功')

            // 判断是否还有需要上传的图片

            if (currentIndx + 1 < imgPaths.length) {

                // 继续上传下一张图片

                t.oneUpload(imgPaths, currentIndx + 1)

            } else {

                console.log('所有图片上传成功')

            }

        },

        fail: function (res) {

            console.log('第' + (currentIndx + 1) + '张图片上传失败')

        }

    })

}


评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

真正的个人免签约支付接口

云免签H5支付