微信小程序云开发实现多张图片上传
功能:
1.实现多次选取图片
2.长按图片删除
思路:
使用数组,将所有图片的临时地址存放起来,再使用循环一个一个图片上传
直接上代码:
//选择图片部分
uploadDetailImage: function() { //这里是选取图片的方法
var that = this;
if (detailPics.length >= that.data.count) {
wx.showToast({
title: '最多选择' + that.data.count + '张!',
})
return;
}
wx.chooseImage({
count: that.data.count - detailPics.length, // 最多可以选择的图片张数,默认9
sizeType:'compressed', // original 原图,compressed 压缩图,默认二者都有
success: function(res) {
var imgs = res.tempFilePaths;
for (var i = 0; i < imgs.length; i++) {
detailPics.push(imgs[i])
}
that.setData({
detailPics:detailPics
})
},
})
},
/**长按删除 */
bindlongpressimg(e){
let that = this
var deleID = e.currentTarget.dataset.id //获取点击项目的内容
console.log(deleID)
detailPics.splice(deleID,1)
that.setData({
detailPics:detailPics
})
},
/**上传 */
//多张图片上传
upLode: function() {
wx.showLoading({
title: '上传中...',
mask: true,
})
for(let i=0; i<detailPics.length; i++){
wx.cloud.uploadFile({
cloudPath: 'manage/record/' + i + '.png', // 上传至云端的路径
filePath: detailPics[i], // 小程序临时文件路径
success: res => {
// 返回文件 ID
console.log("上传成功" + res.fileID)
},
fail: console.error
})
}
wx.hideLoading()
},
效果图:
完整代码链接:微信小程序云开之发多张图片上传.