小程序上傳圖片時,對應的uploadFile接口一次只能上傳一張,這里選擇后台提供一個專門上傳圖片的接口上傳圖片后返回鏈接,再調用比如說評論接口,將返回的圖片鏈接帶上后,再一起上傳。
uploadImg (pictures) { if (pictures.length) { const initNum = pictures.length let succNum = 0 //統計上傳圖片成功的數量 const token = Taro.getStorageSync('token') pictures.map((pic: any) => { Taro.uploadFile({ url: 'upload_img', filePath: pic.url, name: 'img', header: { 'Authentication': token, }, }).then((res: any) => { if (res.data.status === 'SUCC') { let uploadImgs: string[] = this.state.uploadImgs uploadImgs.push(res.data.imgurl) this.setState({ uploadImgs, }, () => { succNum += 1 if (initNum === succNum) {
// 調用評論上傳方法 this.httpComment()
} }) } else { $toast('上傳圖片失敗,請重試!') return false } }) }) } }