uni-app:使用uni.downloadFile下载文件并保存到手机

本文详细介绍了在uni-app跨平台开发框架中如何下载并保存文件。首先通过 uni.downloadFile 获取文件的临时路径,然后使用 uni.saveFile 将临时文件保存至本地,最后通过 uni.openDocument 打开已保存的文件,确保下载过程完整且成功。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在uni-app中下载文件需要先获取文件的临时路径,然后再进行保存,才算下载成功:

// #ifdef APP-PLUS
				const downloadTask = uni.downloadFile({
						url: 'http://xxxxxxxx.com.cn/'+url, //仅为示例,并非真实的资源
							success: (res) => {
								if (res.statusCode === 200) {
									console.log('下载成功');
								}
								let that = this;
								uni.saveFile({
									tempFilePath: res.tempFilePath,
										success: function(red) {
											uni.openDocument({
												filePath:red.savedFilePath,
												success: (sus) => {
													console.log('成功打开')
												}
											})
											console.log(red)
										}
									});
								}
							})
							downloadTask.onProgressUpdate((res) => {
								console.log('下载进度' + res.progress);
								console.log('已经下载的数据长度' + res.totalBytesWritten);
								console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
						})
				//#endif
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流丶年丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值