Code for upload iamges:
chooseImage: choose the images to upload
previewImage: preview the image and enable slide
chooseImage() { wx.chooseImage({ count: 3, sizeType: ['compressed'], sourceType: ['album', 'camera'], success: res => { let currentImages = res.tempFilePaths this.setData({ images: currentImages }) }, }) }, previewImg(event) { let target = event.currentTarget let src = target.dataset.src wx.previewImage({ current: src, urls: this.data.images }) },
Upload to the server:
uploadImage(cb) { let commentImages = this.data.commentImages let images = [] if (commentImages.length) { let length = commentImages.length for (let i = 0; i < length; i++) { wx.uploadFile({ url: config.service.uploadUrl, filePath: commentImages[i], name: 'file', success: res => { let data = JSON.parse(res.data) length-- if (!data.code) { images.push(data.data.imgUrl) } if (length <= 0) { cb && cb(images) } }, fail: () => { length-- } }) } } else { cb && cb(images) } }, onInput(event) { this.setData({ commentValue: event.detail.value.trim() }) }, chooseImage() { let currentImages = this.data.commentImages wx.chooseImage({ count: 3, sizeType: ['compressed'], sourceType: ['album', 'camera'], success: res => { currentImages = currentImages.concat(res.tempFilePaths) let end = currentImages.length let begin = Math.max(end - 3, 0) // latest three pictures currentImages = currentImages.slice(begin, end) this.setData({ commentImages: currentImages }) }, }) },