wx.uploadfile 使用for循环发生异步提交怎么解决?
发布于 5 年前 作者 tao69 6923 次浏览 来自 官方Issues

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);

}

});

}

4 回复

promise了解一下

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

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

递归上传

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) + ‘张图片上传失败’)

        }

    })

}

回到顶部