uni-app开发公众号时安卓端无法一次上传多个图片
uni-app开发公众号时安卓端无法一次上传多个图片
代码示例
ChooseImage() {
let that = this
uni.chooseImage({
count: Number(that.Imglength), //默认9
sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: ['album','camera'], //从相册选择
success: (res) => {
let path = res.tempFilePaths
if (path) {
for (var i = 0; i < path.length; i++) {
uni.uploadFile({
url: Api.url.imguploadUrl, //仅为示例,非真实的接口地址
header: {
'token': uni.getStorageSync("token")
},
filePath: path[i],
name: 'file',
success: (uploadFileRes) => {
if (uploadFileRes.statusCode == 200) {
let imgData = JSON.parse(uploadFileRes.data);
let file={ name:imgData.fileName,url:imgData.id,fileType:imgData.fileType }
that.file.push(file)
this.$emit("input",JSON.stringify(that.file));
uni.showToast({
title: '上传成功',
icon: 'none'
})
}
}
})
}
}
},
});
},
更多关于uni-app开发公众号时安卓端无法一次上传多个图片的实战教程也可以访问 https://www.itying.com/category-93-b0.html
3 回复
问题解决了吗
在uni-app的H5端(包括公众号环境),uni.chooseImage选择多张图片后,安卓设备上确实可能遇到无法一次性上传多张图片的问题。这通常是由于浏览器或WebView对并发上传请求的限制导致的。
从你的代码看,你在success回调中通过循环直接调用了多次uni.uploadFile,这会在短时间内发起多个并发上传请求。在安卓的WebView环境中,可能会被限制或阻塞。
解决方案:
-
改为串行上传:将并发的
uni.uploadFile改为逐个顺序上传,避免并发限制。可以使用递归或async/await实现。 -
使用Promise链控制流程:通过Promise确保上一张图片上传完成后再开始下一张。
修改后的代码示例:
async ChooseImage() {
let that = this;
const res = await uni.chooseImage({
count: Number(that.Imglength),
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera']
});
const paths = res.tempFilePaths;
if (!paths || paths.length === 0) return;
for (let i = 0; i < paths.length; i++) {
try {
const uploadRes = await new Promise((resolve, reject) => {
uni.uploadFile({
url: Api.url.imguploadUrl,
header: { 'token': uni.getStorageSync("token") },
filePath: paths[i],
name: 'file',
success: resolve,
fail: reject
});
});
if (uploadRes.statusCode === 200) {
const imgData = JSON.parse(uploadRes.data);
const file = {
name: imgData.fileName,
url: imgData.id,
fileType: imgData.fileType
};
that.file.push(file);
this.$emit("input", JSON.stringify(that.file));
}
} catch (error) {
console.error(`第${i+1}张图片上传失败:`, error);
continue; // 继续上传下一张
}
}
uni.showToast({
title: '上传完成',
icon: 'none'
});
}


