评论

小程序上传图片(多图)通用方法,数量、尺寸限制

微信小程序通用上传图片方法,数量、尺寸限制

async function chooseAndUploadImg(imgNum = 1, fileMaxSize = 1024 * 2,
	respCallback = function(ret) {
		return ret
	}) {
	return new Promise((resolve, reject) => {
		wx.chooseImage({
			count: imgNum,
			sourceType: ['album'],
			success(ret) {
				const tempFilePaths = ret.tempFilePaths;
				let num = 1;
				let urls = [];
				// 开始上传
				// console.log('tempFilePaths', ret)
				tempFilePaths.forEach(async (path, pathIdx) => {
					let kb = ret.tempFiles[pathIdx].size / 1000;
					let limit = fileMaxSize;
					let ext = path.split('.').pop()
					if (limit && kb > limit) {
						wx.showToast({
							icon: 'none',
							title: '图片' + num + '超' + limit + 'kb,已忽略!'
						});
					} else {
						wx.showLoading({
							mask: true,
							title: '正在上传 ' + num + '/' + tempFilePaths.length
						});

						let {
							fileUrl
						} = await 你自己的上传方法({
							filePath: path
						}).catch(errHandle-自定义的err方法)
						urls.push(fileUrl)
						uni.hideLoading();
						if (urls.length === tempFilePaths.length) {
							resolve(urls)
						}
					}
				});
			},
			fail(ret) {
				console.log('选择这里失败了')
				reject(ret);
			}
		})
	}) // Promise end

}
最后一次编辑于  2022-12-07  
点赞 2
收藏
评论
登录 后发表内容