1 回复
uploadImage: function (data) {
var that = this,
i = data.i ? data.i : 0,
successNum = data.successNum ? data.successNum : 0,
failNum = data.failNum ? data.failNum : 0,
params = {};
params.shopId = this.globalData.shopId;
params._app_id = params.app_id = this.getAppId();
params.sessionid = this.getSessionKey();
params.index = i;
params.count = data.path.length;
console.log(params);
var uploadTask = wx.uploadFile({
url: data.url,
filePath: data.path[i].url,
name: 'fileData',//这里根据自己的实际情况改
formData: params,
success: function(res) {
var result = JSON.parse(res.data);
if(result.status == 0){
successNum+=1;
}
typeof data.success == 'function' && data.success(i,data.path[i].url);
},
fail: function(res){
console.log(res+'shib');
failNum++;
console.log('fail:' + i + "fail:" + failNum);
typeof data.fail == 'function' && data.fail(i,data.path[i].url);
},
complete: function() {
i++;
if (i == data.path.length) { //当图片传完时,停止调用
console.log('执行完毕');
console.log('成功:' + successNum + " 失败:" + failNum);
typeof data.complete == 'function' && data.complete();
} else {//若图片还没有传完,则继续调用函数
data.i = i;
data.successNum = successNum;
data.failNum = failNum;
that.uploadImage(data);
}
}
});
typeof data.progressCallback == 'function' && data.progressCallback(uploadTask,i);},
计算第几张图片的位置,然后递归调用