收藏
回答

wx.uploadFile 上传文件 调用多次 会导致微信小程序爆内存,微信出现无响应闪退?

<template>
	<view class="content">
		<u-button type="warning" @click="chooseVideo">选视频</u-button>
		<u-button type="warning" @click="delVideo">取消</u-button>
	</view>
</template>


<script>
	import {uploadUrl} from '../../config/env.js';
	import {mapGetters} from 'vuex';
	export default {
		data() {
			return {
				uploadVideoUrl: uploadUrl + '/uploadapi/video',
			};
		},
		methods: {
			//删除已经上传的视频
			delVideo(){
				this.uploadVideo = {};
				this.uploadPercent = 0;
				this.uploadVideoPath = '';
				this.uploadTask.offProgressUpdate();
				this.uploadTask && this.uploadTask.abort();
				this.uploadTask = null;
				this.socketTask && this.socketTask.close({
					code: 1000
				});
				this.socketTask = null;
				this.heartBeatTimer && clearInterval(this.heartBeatTimer);
				this.heartBeatTimer = null;
				
			},
			chooseVideo(){
				//选择视频
				uni.showLoading({
					title: '读取视频中'
				});
				this.uid = this.$u.guid();
				uni.chooseVideo({
					count: 1,
					sourceType: ['camera', 'album'],
					compressed: false,
					success: (res)=>{
						console.log(res);
						if(res.size > 524288000){
							return uni.showToast({
								icon: 'none',
								title: '所选的视频超出500M,无法上传!'
							})
						};
						this.uploadPercent = 0;
						uni.showLoading({
							title: '解析视频中'
						});
						this.uploadVideoPath = res.tempFilePath;
						//上传视频
						this.uploadTask = uni.uploadFile({
							url: this.uploadVideoUrl, //仅为示例,非真实的接口地址
							filePath: res.tempFilePath,
							name: 'upfile',
							formData: {
								uId: this.uid
							},
							timeout: 7200000,
							success: (uploadFileRes) => {
								this.uploadVideo = JSON.parse(uploadFileRes.data).data;
								uni.showToast({
									icon: 'none',
									title: '上传成功,可以发布'
								});
							},
							fail: (error)=>{
								console.log(error);
								if(error.errMsg == 'uploadFile:fail abort'){
									uni.showToast({
										icon: 'none',
									    title: '您取消了视频上传!',
									    duration: 2000
									});
								}else{
									uni.showToast({
										icon: 'none',
									    title: '视频上传失败,请重试!',
									    duration: 2000
									});
								};
								//结束上传任务
								this.uploadVideo = {};
								this.uploadPercent = 0;
								this.uploadVideoPath = '';
								this.uploadTask && this.uploadTask.abort();
								this.uploadTask = null;
								this.socketTask && this.socketTask.close({
									code: 1000
								});
								this.socketTask = null;
								this.heartBeatTimer && clearInterval(this.heartBeatTimer);
								this.heartBeatTimer = null;
							},
							complete: () => {
								//关闭socketTask
								this.socketTask && this.socketTask.close({
									code: 1000
								});
								this.socketTask = null;
								this.heartBeatTimer && clearInterval(this.heartBeatTimer);
								this.heartBeatTimer = null;
								this.uploadTask && this.uploadTask.abort();
								this.uploadTask = null;
							}
						});
						this.uploadTask.onProgressUpdate((res)=>{
							uni.showLoading({
								title: '解析进度' + res.progress + '%'
							});
							if(res.progress >= 100){
								uni.hideLoading();
								this.uploadTask.offProgressUpdate()
							}
						})
					},
					fail: (error) => {
						uni.hideLoading();
						//关闭socketTask
						this.socketTask && this.socketTask.close({
							code: 1000
						});
						this.heartBeatTimer && clearInterval(this.heartBeatTimer);
					},
				});
			},
		}
	}
</script>


<style lang="scss" scoped>


</style>

用的是uniapp框架,虽然调用接口前缀是uni.但是实测换成wx.uploadFile()仍然是会出现这种情况
多次上传之后就出现内存爆满,微信闪退,真机测试的是华为荣耀v30 系统是鸿OS2.0
回答关注问题邀请回答
收藏

1 个回答

  • 社区技术运营专员-Jahozheng
    社区技术运营专员-Jahozheng
    2021-07-25

    请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

    2021-07-25
    有用
    回复
登录 后发表内容