uniapp,实现下载文档,并保存到本地,打开文档预览


<u-button class="btn" @click="Download">下载签订合同u-button>



<a href="http://192.168.0.200:8058/files/200200711.doc" class="btn">下载签订合同a>

//签订合同下载
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: '失败请重新下载',
			});
		},
	});
},

你可能感兴趣的:(uniapp)