微信小程序上传文件 chooseMessageFile uploadFile,添加loading显示异常

在uniapp的小程序开发中,遇到chooseMessageFile选择文件后,loading立即消失的问题。原因是chooseMessageFile回调导致页面刷新,中断了loading的显示。为了解决这个问题,开发者在启动loading时添加了500毫秒的延时,确保loading在文件上传成功后才关闭。这样实现了在文件上传过程中展示loading的效果。

 

开发环境:uniapp vue2.0

开发背景:小程序中使用了chooseMessageFile,选中聊天记录中的文件之后调用uploadFile进行上传,本想在调用chooseMessageFile 之前添加loading,在上传成功后在隐藏loading。实际是直接点击选择文件那一下,loading就迅速消失了,没有等上传成功后再消失。

问题原因:调用chooseMessageFile时候,选中文件返回时候,页面进程被中断了,导致loading直接就消失了。

解决办法:在启动loading时候加个延迟,这样就能完美解决了

		updateResume() {
				const that = this;
				this.loading = true;
				wx.chooseMessageFile({
					type: 'file',
					count: 1,
					extension: ['doc', 'docx', 'pdf', 'png', 'jpg', 'xlsx', 'xls'],
					success(res) {
						console.log('msgfile', res);
						// 处理loading过快消失的bug,可能和微信执行的顺序有关,打开上传文件会刷新页面
						setTimeout(() => {
							uni.showLoading({
								title: '简历上传中...',
								mask: true
							})
						}, 500)
						console.log('that.$u.api.getUploadCVFileUr', that.$u.api.getUploadCVFileUr);
						wx.uploadFile({
							url: that.$u.api.getUploadCVFileUrl,
							//url: 'https://25y.newland.com.cn/test/recruit/api/RecruitingTalentCv/uploadCVFile', //仅为示例,非真实的接口地址 该接口返回的的JSON
							filePath: res.tempFiles[0].path,
							name: "file",
							header: {
								"content-type": "application/x-www-form-urlencoded",
								xToken: that.$store.state.vuex_token
							},
							formData: {
								type: 'common'
							},
							success(data) {
								// 微信返回的数据被转成string
								const curData = JSON.parse(data.data);
								if (curData.code === 200) {
									that.form.lastCV.cvUrl = JSON.parse(data.data).result.path;
									that.form.lastCV.cvName = res.tempFiles[0].name;
									that.form.lastCV.cvSize = (res.tempFiles[0].size / 1024).toFixed(2);
								} else {
									that.$u.toast(decodeURIComponent(curData.msg), 3000);
								}
								uni.hideLoading();
								// uni.hideLoading();
							},
							fail(err) {
								uni.hideLoading();
							}

						})
					},
					fail(err) {
						uni.hideLoading();
					}
				})
			},

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时光机上敲代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值