微信小程序(原生)和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();
		}
	});
}
相关推荐
流烟默2 小时前
vue和微信小程序处理markdown格式数据
前端·vue.js·微信小程序
近冬的阳光4 小时前
PDF文档管理系统V2.0
pdf
Driver_tu4 小时前
在windows10上基于Python部署marker,实现PDF转markdown文件(保姆级)
pdf
黄铎彦4 小时前
使用GDI+、文件和目录和打印API,批量将图片按文件名分组打包成PDF
c++·windows·pdf
梅如你4 小时前
IEEE官方期刊缩写查询pdf分享
pdf
家里有只小肥猫5 小时前
uniApp小程序保存canvas图片
前端·小程序·uni-app
小徐_23335 小时前
从空白到交互:用Trae快速搭建uni-app AI对话小程序页面
前端·uni-app·trae
乔冠宇7 小时前
微信小程序中将图片截图为正方形(自动居中)
微信小程序·小程序·typescript·uniapp
貂蝉空大8 小时前
uni-app开发安卓和ios app 真机调试
android·ios·uni-app
程楠楠&M8 小时前
uni-app(位置1)
前端·javascript·uni-app·node.js