uni-app小程序:文件下载打开文件方法苹果安卓都适用

api:

复制代码
const filetype = e.substr(e.lastIndexOf('.')+1)//获取文件地址的类型
console.log('文档',filetype)
uni.downloadFile({
	url: e,//e是图片地址
    success(res) {
	console.log(res)
	if (res.statusCode === 200) {
		console.log('下载成功',);
		var filePath = encodeURI(res.tempFilePath);//这里直接复制不用改值
		uni.openDocument({
		    filePath: encodeURI(filePath),
		    fileType: filetype,//这样文件地址是什么类型,api打开的就是什么类型了,前提是uni.openDocument支持的类型
			showMenu: true,
			success: function(res) {
				console.log('打开文档成功');
				  },
				fail(e) {
					console.log('打开失败',e)
						}
						   });
						} else {
						    console.log('下载失败');
					    }
					}
				});

encodeURI的作用:

encodeURI函数将URI中的特殊字符转换为它们的UTF-8编码表示形式,以确保它们在传输和处理过程中不会被误解或错误解析。这样可以避免因特殊字符导致的错误或安全问题。

源码:

运行效果:

点击文件1就可以下载打开文件预览了

相关推荐
阿华的代码王国2 小时前
【Android】卡片式布局 && 滚动容器ScrollView
android·xml·java·前端·后端·卡片布局·滚动容器
风起云涌~2 小时前
【Android】桌面小组件开发
android·gitee
双鱼大猫4 小时前
从传统播放器到AI智能体:Xplayer 2.0的技术革新之路
android
CYRUS_STUDIO4 小时前
动态篡改 so 函数返回值:一篇带你玩转 Android Hook 技术!
android·c++·逆向
xzkyd outpaper4 小时前
Android中主线程、ActivityThread、ApplicationThread的区别
android·面试
就叫飞六吧5 小时前
mysql全量备份、全量恢复demo
android·mysql·adb
PenguinLetsGo5 小时前
关于 Android16 MOPS 函数指令非法问题
android
xzkyd outpaper7 小时前
Kotlin中Flow
android·开发语言·kotlin
byte轻骑兵8 小时前
【Bluedroid】bta_av_sink_media_callback(BTA_AV_SINK_MEDIA_CFG_EVT)流程源码分析
android·c++·bluedroid
weixin_lynhgworld8 小时前
盲盒抽卡机小程序系统开发:连接线上线下娱乐新桥梁
小程序·娱乐