uniapp 下载文件到手机

下载后端传递过来的文件

javascript 复制代码
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();
		}
	}
});
相关推荐
小李科技20 分钟前
【性能优化】事件委托:目标定位与 React/Vue 实战
前端·vue.js·react.js·面试·性能优化·前端框架
QianhangQianping27 分钟前
一块大屏的数据在浏览器里怎么变成画面:背景、子屏、前景三层结构
前端·低代码·架构·编辑器·数据可视化·大屏可视化
梦想很大很大38 分钟前
让 Codex 自由发挥,给 nvm-desktop 换个 UI
前端·人工智能·设计
蓝莓味的口香糖42 分钟前
【VUE】【CSS】Vue移动端适配主流方案总结
前端·css·vue.js
shennv88843 分钟前
接入大模型流式输出后,文字偶尔乱码?问题可能出在前端
前端
csj5044 分钟前
前端基础之《React(16)—Modal组件封装》
前端·react.js
雨季mo浅忆1 小时前
Vue关于前端XSS漏洞修复方案
前端·xss
上火的金鱼妹1 小时前
关于多进程sse连接通知的实战
javascript·状态模式·sse
切糕师学AI1 小时前
el-select-v2 深度解析:用虚拟滚动解决海量数据下拉选择性能瓶颈
前端·vue.js·elementui·el-select-v2
徐小夕1 小时前
JitOffice:把办公、AI 与审批装进同一个平台
前端·vue.js·github