微信小程序(原生)和uniapp预览电子文件doc/pdf/ppt/excel等

微信小程序原生预览文件

javascript 复制代码
function previewFile(value) {
	const fileExtName = `${value.ext}`;
	const randFile = new Date().getTime() + fileExtName;
	uni.showLoading({
		title: '加载中...'
	})

	wx.downloadFile({
		url: value.url, // 文件的本身url
		filePath: wx.env.USER_DATA_PATH + '/' + randFile, // 本地自定义的文件名
		success: function(res) {
			// console.log("filePath", res)
			let filePath = res.filePath; // 微信临时文件路径(这里要使用自定义的名字文件名,否则打开的文件名是乱码)
			let fileType = value.ext.substring((1));
			wx.openDocument({
				filePath: filePath,
				fileType: fileType,
				// showMenu: true,  // 是否显示右上角菜单按钮 默认为false(看自身需求,可要可不要。后期涉及到右上角分享功能)
				success: function() {
					// console.log("打开文件成功")
				},
				fail: function() {
					// console.log("打开文件失败,请稍后重试")
				}
			});
			uni.hideLoading();
		},
		fail: function() {
			// console.log("请稍后重试")
		}
	});
}

uniapp预览电子文件

javascript 复制代码
function previewFile(value) {
	let fileType = value.ext.substring((1));   //去掉点 如下.doc=>doc
	uni.showLoading({
		title: '加载中...'
	})
	uni.downloadFile({
		url: value.url,
		success: function(res) {
			let filePath = res.tempFilePath;
			uni.openDocument({
				filePath: filePath,
				fileType,
				success: function(res) {
					// console.log('打开文档成功');
				}
			});
			uni.hideLoading();
		}
	});
}
相关推荐
2501_915921436 小时前
iOS 是开源的吗?苹果系统的封闭与开放边界全解析(含开发与开心上架(Appuploader)实战)
android·ios·小程序·uni-app·开源·iphone·webview
2501_915909069 小时前
原生 iOS 开发全流程实战,Swift 技术栈、工程结构、自动化上传与上架发布指南
android·ios·小程序·uni-app·自动化·iphone·swift
2501_915106329 小时前
Comodo HTTPS 在工程中的部署与排查实战(证书链、兼容性与真机抓包策略)
网络协议·http·ios·小程序·https·uni-app·iphone
2501_9159090610 小时前
苹果软件混淆与 iOS 代码加固趋势,IPA 加密、应用防反编译与无源码保护的工程化演进
android·ios·小程序·https·uni-app·iphone·webview
2501_9160074710 小时前
苹果软件混淆与 iOS 应用加固实录,从被逆向到 IPA 文件防反编译与无源码混淆解决方案
android·ios·小程序·https·uni-app·iphone·webview
前端开发呀14 小时前
无所不能的uniapp拦截器【三】uni-app 拦截器核心流程解析
前端·javascript·微信小程序
尘似鹤14 小时前
微信小程序学习(六)--多媒体操作
学习·微信小程序·小程序
UpYoung!15 小时前
无广技术贴!【PDF编辑器】Solid Converter PDF保姆级图文下载安装指南——实用推荐之PDF编辑软件
学习·数学建模·pdf·编辑器·运维开发·个人开发
Uluoyu15 小时前
支持Word (doc/docx) 和 PDF 转成一张垂直拼接的长PNG图片工具类
java·pdf·word