uni-app项目uni.uploadFile多文件上传时适配微信小程序记录
·
以前使用uni-app提供的异步api一直都用系统提供的使用方式,将回调写到success:函数里面,今天遇到图片上传的问题,且微信小程序不支持files参数,为了实现多张图片同时异步上传,且当所有图片上传成功时执行回调函数,将系统api包装如下:
upload(img) {
return new Promise((resolve, reject) => {
uni.uploadFile({
//参数可查看官方文档
url: 'https://dev.upload/upload',
filePath: img,
name: 'file',
header: {
"Content-Type": "multipart/form-data",
"accept": 'application/json'
},
success: (res) => {
//因为我用的三方存储,所以将存储后的返回路径做了存储,
//可能不同的accept返回的数据格式不同,所以处理数据的方式也不同
//我在此处将上传三方库成功返回的路径进行了压栈存储
//uploadImgs.push(JSON.parse(res.data).data.code)
resolve(res)
}
})
})
},
处理多张图片上传我选择了遍历images,然后执行upload
//res 是本地图片images遍历上传以后返回的promise对象的集合
let res = images.map(img => this.upload(img))
//最后调用Promise.all
Promise.all(res).then(res => {
//所有图片上传成功时所需执行代码块
//比如可以将upload函数中success 回调里压栈的数据发送给后台
})
更多推荐



所有评论(0)