uniapp下载文件

569 阅读1分钟
// 下载
downLoadFile(filePath) {
	//下载文件,生成临时地址
	uni.showLoading({
		title: '下载中...',
	})
	uni.downloadFile({
		url: filePath,//下载地址接口返回
		success: (data) => {
			if (data.statusCode === 200) {
				//文件保存到本地
				uni.saveFile({
					tempFilePath: data.tempFilePath, //临时路径
					success: function (res) {
						//打开文档查看
						uni.openDocument({
							filePath: res.savedFilePath,
							success: function (res) {
								uni.hideLoading()
							},
							fail: (err) => {
								uni.hideLoading()
								uni.showToast({
									icon: 'none',
									title: '打开文件'
								});
							}
						});
					},
					fail: (err) => {
						uni.hideLoading()
						uni.showToast({
							icon: 'none',
							title: '保存失败'
						});
					}
				});
			}
		},
		fail: (err) => {
			uni.hideLoading()
			uni.showToast({
				icon: 'none',
				title: '下载失败'
			});
		}
	});
},