微信小程序(原生)和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();
		}
	});
}
相关推荐
w20180016 小时前
申论答题纸模板大作文格子纸及行测答题卡PDF可打印
pdf
2401_8769641319 小时前
27考研396经济类联考历年真题PDF
考研·pdf
前端小木屋19 小时前
uniapp与蓝牙设备连接详细步骤
前端·微信小程序
2401_8769641321 小时前
27唐迟阅读方法论|思维导图PDF
pdf
2501_9160074721 小时前
iOS应用性能优化全面指南:从内存管理到工具使用
android·ios·性能优化·小程序·uni-app·iphone·webview
2401_8769641321 小时前
27唐迟长难句的逻辑PDF
pdf
Web打印1 天前
HttpPrinter(web打印控件)的gridreport和Fastreport对 ☑、★、✓ 等 Unicode 符号的支持
chrome·pdf·web
Web打印1 天前
HttpPrinter(web打印控件)的gridreport导出pdf,字体模糊的解决方法
pdf
Web打印1 天前
2027年Web打印的几种方法
前端·pdf·web