小程序下载视频到本地

小程序下载视频到本地

wxml代码

<view class="button_view">下载视频</view>

wxss代码

.button_view{
margin-top:40rpx;
width:100%;
height:90rpx;
line-height:90rpx;
text-align: center;
	background: chocolate;
	font-size: 30rpx;
	color: #fff;
}

js代码

data: {
		video_url:'',//视频路径
	},
	// 下载
	download_fun: function (e) {
		var that = this
		if (!wx.saveImageToPhotosAlbum) {
			wx.showModal({
				title: '提示',
				content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
			})
			return;
		}
		// 检查用户授权
		wx.getSetting({
			success(res) {
				if (!res.authSetting['scope.writePhotosAlbum']) {
					// 接口调用询问  
					wx.authorize({
						scope: 'scope.writePhotosAlbum',
						success() {
								that.dow_video()
						},
						fail() {
							// 用户拒绝授权
								wx.showModal({
									title: '保存视频',
									content: '保存视频需要您授权',
									showCancel: true,
									confirmText: '确定',
									success(res) {
										if (res.confirm) {
											// 打开设置页面  
											wx.openSetting({
												success(res) {
													if (res.authSetting['scope.writePhotosAlbum']) {
														that.dow_video()
													} else {
														wx.showToast({
															title: '授权失败!',
															icon: "none"
														});
													}
												},
												fail(res) {
													wx.showToast({
														title: '授权失败!',
														icon: "none"
													});
												}
											});
										} else if (res.cancel) {
											wx.showToast({
												title: '您已取消授权!',
												icon: "none"
											});
										}
									}
								})
							
						}
					})
				} else {
						that.dow_video()
				}
			},
			fail(res) {
				console.log(res);
			}
		})
	},
	// 下载视频
	dow_video: function () {
		var that = this;
		wx.showLoading({
			title: '下载视频中',
		})
		// 用户已经同意小程序使用相册的授权
		wx.downloadFile({
			url: that.data.video_url,//要下载的视频路径
			success: function (res) {
				that.setData({
					is_button: true
				})
				var temp = res.tempFilePath;//获取临时文件路径
				wx.saveVideoToPhotosAlbum({
					filePath: temp,
					success: function () {
						wx.hideLoading()
						wx.showToast({
							title: '视频保存成功',
							duration: 3000,
							icon: 'none'
						})
					},
					fail: function () {
						wx.hideLoading()
					}
				})
			},
			fail: function (res) {
				wx.hideLoading()
				wx.showToast({
					icon: 'none',
					title: '获取视频临时路径失败',
				})
			}
		})
	},

ps:样式随便写的,主要实现功能

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值