天天看点

mysql多图怎么同时上传_小程序如何同时上传多张图片?

元素上bindtap方法:(选择图片)

chooseImgHandler: function (e) {

var that = this;

that.count_img = count_img; //允许上传的图片数目

if (that.isGoing) return;

that.isGoing = true;

if (count_img) {

wx.chooseImage({

count: count_img,

sizeType: ['compressed'],

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

success: function (res) {

var tempFilePaths = res.tempFilePaths;

wx.showLoading();

that.uploadimg(tempFilePaths);

},

fail: function () {

// App.serverErrorTip("图片选择失败...");

that.isGoing = false;

}

})

} else {

App.serverErrorTip('已达到上传图片上限啦~');

that.isClicking = false;

}

},

//拿到用户选择的图片的临时文件路径数组以后,循环上传到自己的服务器

//上传多图

uploadimg: function (data) {

var that = this,

zero = 0,

i = data.i ? data.i : zero,

success = data.success ? data.success : zero,

fail = data.fail ? data.fail : zero,

count_img = that.count_img;

wx.uploadFile({

url: '自己服务器的地址',

filePath: data[i],

name: 'file',

formData: {服务器需要接受的参数},

success: function (resp) {

if (resp.statusCode == 200) {

if (!resp.data || resp.data.indexOf("errcode") != -1) {

var msg = "";

try {

var json = eval("(" + resp.data + ")");

msg = '上传图片失败:' + json.errmsg;

} catch (e) {

msg = '上传图片失败'

}

that.wetoast.toast({

title: msg,

duration: 3000

})

fail++;

wx.hideLoading();

} else {

//成功后的回调

//自己的事件方法

that.count_img = count_img;

if (i == (data.length - 1)) {

wx.hideLoading();

}

}

} else {

if (i == (data.length - 1)) {

wx.hideLoading();

}

App.serverErrorTip('上传图片失败');

}

},

fail: function (res) {

fail++;

if (i == (data.length - 1)) {

wx.hideLoading();

}

},

complete: function () {

i++;

if (i == data.length) { //当图片传完时,停止调用

console.log('执行完毕');

console.log('成功:' + success + " 失败:" + fail);

that.isClicking = false;

} else {//若图片还没有传完,则继续调用函数

data.i = i;

data.success = success;

data.fail = fail;

that.uploadimg(data);

}

}

});

},