0
点赞
收藏
分享

微信扫一扫

【uniapp】微信运行报错TypeError_ Cannot read property ‘FormData‘ of undefined

文风起武 2023-11-28 阅读 40

注意:
1、定义:fileList,

2、图片上传请求地址按照自己的修改
 
async afterReadList(event) {  
    let lastFileIndex = this.fileList.length - 1;  
    let remainingCount = lastFileIndex > 9 ? 0 : 10 - lastFileIndex;  
  
    try {  
        let chooseImageRes = await uni.chooseImage({  
            count: remainingCount,  
            sizeType: ['original', 'compressed'],  
            sourceType: ['album'],  
        });  
          
        let files = chooseImageRes.tempFilePaths;  
        let fileListLength = this.fileList.length;  
        let uploadCount = files.length == 1 ? files.length : 11 - fileListLength;  
          
        let uploadPromises = [];  
        for (let i = 0; i < uploadCount; i++) {  
            let file = files[i];  
            uploadPromises.push(new Promise((resolve, reject) => {  
                uni.uploadFile({  
                    url: wx.getStorageSync('apiUrl') + '/common/uploadMinio',  
                    filePath: file,  
                    name: 'file',  
                    formData: {  
                        'user': 'test'  
                    },  
                    header: {  
                        'Authorization': 'Bearer ' + wx.getStorageSync('token')  
                    },  
                    success: (uploadFileRes) => {  
                        let res = JSON.parse(uploadFileRes.data);  
                        if (res.code == '200') {  
                            this.fileList.unshift({  
                                url: res.url,  
                                uid: res.url  
                            });  
                            resolve();  
                        } else {  
                            let x = this.fileList.length - 1;  
                            this.fileList.splice(x, 1);  
                            reject();  
                        };  
                    },  
                    fail: (error) => {  
                        reject(error);  
                    }  
                });  
            }));  
        }  
        await Promise.all(uploadPromises);  
    } catch (error) {  
        console.error('Error while uploading files', error);  
    }  
},

举报

相关推荐

0 条评论