小程序上传多张图片

小程序上传图片时,对应的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 } }) }) } }
原文地址:https://www.cnblogs.com/pjl43/p/10507670.html