<!-- #ifdef  APP-PLUS -->
<u-button class="btn" @click="Download">下载签订合同</u-button>
<!-- #endif -->

<!-- #ifdef  H5 -->
<a href="http://192.168.0.200:8058/files/200200711.doc" class="btn">下载签订合同</a>
<!-- #endif -->
//签订合同下载
Download() {
	uni.downloadFile({
		url: 'http://192.168.0.200:8058/files/200200711.doc',//下载地址接口返回
		success: (data) => {
			if (data.statusCode === 200) {
				//文件保存到本地
				uni.saveFile({
					tempFilePath: data.tempFilePath, //临时路径
					success: function(res) {
						uni.showToast({
							icon: 'none',
							mask: true,
							title: '文件已保存:' + res.savedFilePath, //保存路径
							duration: 3000,
						});
						setTimeout(() => {
							//打开文档查看
							uni.openDocument({
								filePath: res.savedFilePath,
								success: function(res) {
									// console.log('打开文档成功');
								}
							});
						}, 3000)
					}
				});
			}
		},
		fail: (err) => {
			console.log(err);
			uni.showToast({
				icon: 'none',
				mask: true,
				title: '失败请重新下载',
			});
		},
	});
},

2023.3.7:更新

我的另一篇文章,记录了如何把文件下载到手机 并保存到自定义的目录中。有需要的可以去查看文件保存到手机文件内

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐