uni-app图片上传
注意:
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);
}
},