小程序上傳多張圖片


小程序上傳圖片時,對應的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 } }) }) } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM