uniapp 下载文件到手机

文章讲述了在H5环境中,如何利用uni-app的API下载后端传递的文件,包括处理下载过程中的加载提示、文件名修改以及使用uni.openDocument打开文件的步骤。如果文件类型不支持,会给出相应的提示。

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

下载后端传递过来的文件

let thil = this
uni.showLoading({
	title: '下载中',
	mask:true
})
uni.downloadFile({
	url: '接口地址', //仅为示例,并非真实的资源
	header: {
		"Authorization": 'token'
	},
	responseType: 'blob',
	success: (res) => {
		if (res.statusCode === 200) {
			uni.hideLoading();
			//更改文件名称
			// 拿到临时文件的绝对路径
			let filepathss = plus.io.convertLocalFileSystemURL(res.tempFilePath);
			// 通过这个路径来拿到他
			plus.io.resolveLocalFileSystemURL(filepathss, function(entry) {
				const tempFileName = entry.name;
				entry.getParent(function(parentDicEntry) {
					entry.moveTo({
						fullPath: parentDicEntry.fullPath + '/'
					}, `${thil.$u.guid(20)}.zip`, function(newFile) {
						uni.openDocument({
							fileType: 'zip',
							filePath: newFile.fullPath,
							success: function(res2) {

							},
							fail: function(err) {
								if (err.errCode == 1 || err
									.code == 1) {
									uni.showToast({
										title: `未找到能打开xlsx类型文件的手机应用`,
										icon: 'none'
									})
								}
							}
						});
					}, function(moveError) {
						uni.showToast({
							title: `已在第三方应用中打开过,请在第三方应用查看时保存`,
							icon: 'none'
						})
					})
				})
			})
		} else {
			uni.hideLoading();
		}
	}
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值