微信小程序单个上传 多个上传
单个上传
//选择图片 uploadImg() { const that = this; wx.chooseMessageFile({ count: 1, type: 'image', success(res) { // tempFilePath可以作为img标签的src属性显示图片 const tempFilePaths = res.tempFiles; wx.uploadFile({ url: interfaces.uploadeImg, filePath: tempFilePaths[0]['path'], name: 'file', formData: { 'user': 'test' }, success(res) { let data = JSON.parse(res.data); console.log(data) if (data.code == 200) { that.setData({ uuid: data.data.uuid, icon: data.data.link }) } } }) } }) },
多个上传
uploadpicture() { const that = this; let images = that.data.images; wx.chooseMessageFile({ count: 6, type: 'image', success(res) { console.log(res.tempFiles) // tempFilePath可以作为img标签的src属性显示图片 let tempImages = res.tempFiles; if (tempImages.length > 6) { wx.showToast({ title: "最多只能上传6张", icon: 'none' }) return; } for (let i = 0; i < tempImages.length; i++) { wx.uploadFile({ url: interfaces.uploadeImg, filePath: tempImages[i].path, name: 'file', formData: { 'user': 'test' }, success: (res) => { let data = JSON.parse(res.data); if (data.code == 200) { images += "," + data.data.uuid; if (images[0] === ',') { images = images.substring(1); } that.setData({ images }, () => { wx.showLoading({ title: '上传成功', duration: 1000 }) }) } }, fail: (res) => { console.log(res) wx.showToast({ icon: "none", title: '上传图片失败,服务器异常', duration: 2000 }) } }) } } }) },