uniapp 小程序文件下载,图片保存

//文件下载
download(e) {//这个i是上面带的索引位
    uni.showLoading({
    	title:'下载中...'
    })
	uni.downloadFile({
		url: e,//下载地址,后端接口获取的链接
		success: (data) => {
			if (data.statusCode === 200) {
				uni.saveFile({//文件保存到本地
					tempFilePath: data.tempFilePath, //临时路径
					success: function(res) {
						uni.hideLoading()
						uni.showToast({
							icon: 'none',
							mask: true,
							title: '文件已保存:' + res.savedFilePath, //保存路径
							duration: 3000,
						});
						// setTimeout(() => { //可要可不要 这里测试一下是否下载成功
						// 	//打开文档查看
						// 	uni.openDocument({
						// 		filePath: res.savedFilePath,
						// 		success: function(res) {
						// 			console.log('打开文档成功');
						// 		}
						// 	});
						// }, 2000)
					},
					fail(){
						uni.hideLoading()
					}
				});
			}
		},
		fail: (err) => {
			uni.hideLoading()
			uni.showToast({
				icon: 'none',
				mask: true,
				title: '失败请重新下载',
			});
		},
	});
},


//保存图片
save(url){
	const that = this;
	uni.showLoading({
		title: '正在保存图片...'
	});
	//获取用户的当前设置。获取相册权限
	uni.getSetting({
		success: (res) => {
			//如果没有相册权限
			if (!res.authSetting["scope.writePhotosAlbum"]) {
				//向用户发起授权请求
				uni.authorize({
					scope: "scope.writePhotosAlbum",
					success: () => {
						//授权成功保存图片到系统相册
						uni.getImageInfo({
							src: url,
							success: function(image) {
								
								uni.saveImageToPhotosAlbum({
									//图片路径,不支持网络图片路径
									filePath: image.path,
									success: (res) => {
										uni.hideLoading();
										uni.showToast({
											title: "保存成功!",
										});
										that.isNewGift = false;
									},
									fail: (res) => {
										console.log(res.errMsg);
										uni.showToast({
											title: res.errMsg,
										});
									},
									complete: (res) => {uni.hideLoading();},
								});
							}
						})
					},
					//授权失败
					fail: () => {
						uni.hideLoading();
						uni.showModal({
							title: "您已拒绝获取相册权限",
							content: "是否进入权限管理,调整授权?",
							success: (res) => {
								if (res.confirm) {
			//调起客户端小程序设置界面,返回用户设置的操作结果。(重新让用户授权)
									uni.openSetting({
										success: (res) => {
										console.log(res.authSetting);
										},
									});
								} else if (res.cancel) {
									uni.showToast({
										title: "已取消!",
										icon:"none"
									});
								}
							},
						});
					},
				});
			} else {
				uni.getImageInfo({
					src: url,
					success: function(image) {
						//如果已有相册权限,直接保存图片到系统相册
						uni.saveImageToPhotosAlbum({
							filePath: image.path,
							success: (res) => {
								uni.hideLoading();
								uni.showToast({
									title: "保存成功!",
								});
								that.isNewGift = false;
							},
							fail: (res) => {
								uni.hideLoading();
								console.log(res.errMsg);
								uni.showToast({
									title: res.errMsg,
									icon:"none"
								});
							},
							//无论成功失败都走的回调
							complete: (res) => {uni.hideLoading();},
						});
					}
				})
			}
		},
		fail: (res) => {},
	});
},

你可能感兴趣的:(uni-app,小程序,javascript)