uniapp 下载图片到手机相册

<button type="primary" class="btn" @tap="down">下载</button>

先写一个下载按钮到页面上

uniapp官方封装好了下载的方法uni.downloadFile()

        down() {
				uni.request({
					url: "this.urls",//请求接口的地址
					method: 'GET',
					dataType: "json",
					success: res => {
						console.log(res.data[0].pictureurl)
						this.urls = res.data[0].pictureurl
						uni.setStorageSync("urls", res.data[0].pictureurl)
						console.log(this.urls)
						let thats = this
						const downloadTask = uni.downloadFile({
							url: thats.urls,
							success: (res) => {
								console.log(res)
								uni.showToast({
									title: '下载成功',
									icon: 'success'
								})
								let that = this;
								uni.saveImageToPhotosAlbum({
									filePath: res.tempFilePath,
									success: function() {
								  uni.showToast({
											title: "保存成功",
											icon: "none"
										});
									},
									fail: function() {
										uni.showToast({
								  	title: "保存失败,请稍后重试",
											icon: "none"
										});
									}
								});

							}
						});

						downloadTask.onProgressUpdate((res) => {
							console.log('下载进度' + res.progress);
							console.log('已经下载的数据长度' + res.totalBytesWritten);
							console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
						});
					}
				})

			}

我咋这里先请求uni.request的原因是我的下载链接是在接口里,直接调用下载方法uni.downloadFile拿不到下面放置下载链接的参数,所以我先调用uni.request方法先拿取这个参数之后在调用的下载方法。

方法:uni.saveImageToPhotosAlbum是官方文档中有的作用是将下载的图片保存到本机相册中,建议大家在运行测试的时候可以放到模拟器测试,以上方法不支持在H5运行。

官方文档:uni.downloadFile()uni.uploadFile(OBJECT) | uni-app官网

官方文档:uni.saveImageToPhotosAlbumuni.chooseImage(OBJECT) | uni-app官网

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值