在微信小程序中不支持多张图片上传,需要做循环实现多张图片上传
// 官方这边是只能一次上传一张
uni.chooseImage({
success: (chooseImageRes) => {
const tempFilePaths = chooseImageRes.tempFilePaths;
uni.uploadFile({
url: 'https://www.example.com/upload', //仅为示例,非真实的接口地址
filePath: tempFilePaths[0],
name: 'file',
formData: {
'user': 'test'
},
success: (uploadFileRes) => {
console.log(uploadFileRes.data);
}
});
}
});
上传多张(做循环)
uni.chooseImage({
success: (chooseImageRes) => {
const tempFilePaths = chooseImageRes.tempFilePaths;
for(let i = 0;i < tempFilePaths.length; i++) {
uni.uploadFile({
url: 'https://www.example.com/upload', //仅为示例,非真实的接口地址
filePath: tempFilePaths[i],
name: 'file',
formData: {
'user': 'test'
},
success: (uploadFileRes) => {
console.log(uploadFileRes.data);
}
});
}
}
});
如果是app或者h5端,可以设置files实现多张图片上传
uni.chooseImage({
success: (chooseImageRes) => {
const tempFilePaths = chooseImageRes.tempFilePaths;
let imgs = tempFilePaths.map((value, index) => {
return {
name: index,
uri: value
})
uni.uploadFile({
url: 'https://www.example.com/upload', //仅为示例,非真实的接口地址
files: imgs,
success: (uploadFileRes) => {
console.log(uploadFileRes.data);
}
});
}
});
转载于 uniapp中上传多张图片