评论

小程序上传多媒体通用方法

小程序上传多媒体通用方法

async function chooseAndUploadMedia(maxDuration = 15,
	respCallback = function(ret) {
		return ret
	}) {
	return new Promise((resolve, reject) => {
		wx.chooseMedia({
			sourceType: ["album", "camera"],
			mediaType: ['video'],
			maxDuration: maxDuration,
			camera: "back",
			success(ret) {
				const tempFilePaths = ret.tempFiles;
				let num = 1;
				let urls = [];
				// 开始上传
				console.log('tempFilePaths', ret)
				tempFilePaths.forEach(async (path, pathIdx) => {
					wx.showLoading({
						mask: true,
						title: '正在上传 ' + num + '/' + tempFilePaths.length
					});

					let {
						fileUrl
					} = await 你自己的上传方法({
						filePath: path.tempFilePath
					}).catch(自定义的错误提示)

					urls.push(fileUrl)
					wx.hideLoading();
					if (urls.length === tempFilePaths.length) {
						resolve(urls)
					}
				});
			},
			fail(ret) {
				console.log('选择这里失败了', ret)
				reject(ret);
			}
		})
	}) // Promise end


}
async function chooseAndUploadMedia(maxDuration = 15,
	respCallback = function(ret) {
		return ret
	}) {
	return new Promise((resolve, reject) => {
		wx.chooseMedia({
			sourceType: ["album", "camera"],
			mediaType: ['video'],
			maxDuration: maxDuration,
			camera: "back",
			success(ret) {
				const tempFilePaths = ret.tempFiles;
				let num = 1;
				let urls = [];
				// 开始上传
				console.log('tempFilePaths', ret)
				tempFilePaths.forEach(async (path, pathIdx) => {
					wx.showLoading({
						mask: true,
						title: '正在上传 ' + num + '/' + tempFilePaths.length
					});

					let {
						fileUrl
					} = await 你自己的上传方法({
						filePath: path.tempFilePath
					}).catch(自定义的错误提示)

					urls.push(fileUrl)
					wx.hideLoading();
					if (urls.length === tempFilePaths.length) {
						resolve(urls)
					}
				});
			},
			fail(ret) {
				console.log('选择这里失败了', ret)
				reject(ret);
			}
		})
	}) // Promise end


}


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