微信小程序(原生)和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();
		}
	});
}
相关推荐
2401_8979300638 分钟前
微信小程序运行机制详解
微信小程序·小程序
『 时光荏苒 』10 小时前
微信小程序实时日志记录-接口监控
微信小程序·小程序·微信小程序日志·日志抓取
老李不敲代码10 小时前
榕壹云外卖跑腿系统:基于Spring Boot+MySQL+UniApp的智慧生活服务平台
spring boot·mysql·微信小程序·uni-app·软件需求
社会底层无业大学生12 小时前
微信小程序跳
微信小程序·小程序·notepad++
ace_TiAmo14 小时前
React8+taro开发微信小程序,实现lottie动画
微信小程序·小程序·react·taro
不会叫的狼15 小时前
uni-app初学
uni-app
老李不敲代码15 小时前
榕壹云在线商城系统:基于THinkPHP+ Mysql+UniApp全端适配、高效部署的电商解决方案
mysql·微信小程序·小程序·uni-app·软件需求
java_强哥17 小时前
springboot+tabula解析pdf中的表格数据
spring boot·pdf·解析pdf表格
diygwcom17 小时前
uniapp解决上架华为应用市场审核要求-监听权限的申请
uni-app