uni-app 常用操作

图片上传

upload(key){
	uni.chooseImage({
		success: (res) => {
			const file = res.tempFilePaths[0];
			var url = 'XXXXXXXX';
			if (file) {
				this.form[key+'url']=file
				uni.uploadFile({
					url: url, 
					filePath: file,
					name: 'file',
					success: (ures) => {
						this.form[key]=JSON.parse(ures.data).data.id
					}
				});
			}
		}
	});	
},

视频上传

chooseVideo(){
	uni.chooseVideo({
		// maxDuration:0,
		count: 1,
		sourceType: ['camera', 'album'],
		success: (responent) => {
			uni.showLoading();
			let videoFile = responent.tempFilePath;
			uni.uploadFile({
				url:'XXXXXX',
				method:"POST",
				filePath:videoFile,
				formData: {
					uid:uni.getStorageSync('uuid')
				},
				name:'file',
				success: (res) => {   
					console.log(res.data);
					uni.hideLoading()
					uni.showToast({
						icon:'success',
						title: '上传成功'
					});
					// let videoUrls = JSON.parse(res.data) //微信和头条支持
					// let videoUrls = res.data //百度支持
					// this.imagesUrlPath = this.imagesUrlPath.concat(videoUrls.result.filePath);
					// this.src = videoUrls.result.filePath; //微信
				},
				fail: (res) => {
					uni.hideLoading()
					uni.showToast({
						icon:'success',
						title: '上传失败'
					});
				}
			})
			// this.src = responent.tempFilePath;  //头条
		}
	})
}

manifest.json 设置超时时间

"networkTimeout": {
	"request" : 600000,
	"uploadFile" : 600000
}
发布了65 篇原创文章 · 获赞 20 · 访问量 2097

猜你喜欢

转载自blog.csdn.net/weixin_43993175/article/details/104040831