1. 程式人生 > >多張圖片上傳 微信小程式

多張圖片上傳 微信小程式

// 選擇圖片
choose_pic: function() {
var that = this;
var choose_pics = [];


wx.chooseImage({
count: 9, // 預設9
sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,預設二者都有
sourceType: ['camera'], // 可以指定來源是相簿還是相機,預設二者都有
success: function(res) {
that.setData({
tip: "正在上傳照片..."
})
var successUp = 0; //成功個數
var failUp = 0; //失敗個數
var length = res.tempFilePaths.length; //總共個數
var i = 0; //第幾個
that.upload_pics(res.tempFilePaths,successUp,failUp,i,length,choose_pics);
}
})
},
upload_pics:function(filePaths,successUp,failUp,i,length,choose_pics){
var that = this;
wx.uploadFile({
url: url,
filePath: filePaths[i],
name: 'fileData',
success: (resp) => {
successUp++;
},
fail: (res) => {
failUp ++;
},
complete: (res) => {
choose_pics.unshift(res.data);
that.setData({
choose_pics: choose_pics,
tip:''
})
i ++;
if(i == length)
{
app.showT('總共' + successUp + '張上傳成功,' + failUp +'張上傳失敗!','success');
}
else
{
that.upload_pics(filePaths,successUp,failUp,i,length,choose_pics);
}
},
});
},