uniapp在手机端预览文件

javascript 复制代码
handlePdf(url) {
          	uni.showLoading({
					title: '加载中',
					mask: true,
				})
				wx.downloadFile({
					url: url,
					success: function(res) {
						console.log(res)
						uni.hideLoading()
						var filePath = res.tempFilePath
						uni.showLoading({
							title: '正在打开',
							mask: true,
						})
						wx.openDocument({
							filePath: filePath,
							fileType: 'pdf',
							success: function(res) {
								console.log(res)
								uni.hideLoading()
								console.log('打开文档成功')
							},
							fail: function(err) {
								uni.hideLoading()
								console.log('fail:' + JSON.stringify(err))
							},
						})
					},
					fail: function(err) {
						uni.hideLoading()
						console.log('fail:' + JSON.stringify(err))
					},
				})
},

url是文件的地址,使用wx.downloadFile将文件下载到本地,然后将文件使用wx.openDocument进行打开

相关推荐
CodeSheep4 分钟前
宇树科技,改名了!
前端·后端·程序员
Hilaku12 分钟前
为什么我们用了 Vite 还是构建慢?——真正的优化在这几步
前端·javascript·vite
XI锐真的烦12 分钟前
横向对比npm和yarn
前端·npm·node.js
国家不保护废物12 分钟前
🧩 React 组件化进阶:像乐高大师一样搭建你的应用世界!
前端·react.js·ai编程
TimelessHaze19 分钟前
从"切图崽"到前端工程师:React 到底是个啥?🚀
前端·react.js·ai编程
站在风口的猪110824 分钟前
《前端面试题:CSS的display属性》
前端·css·html·css3·html5
wandongle25 分钟前
HTML 面试题错题总结与解析
前端·面试·html
Code_Geo27 分钟前
前端打包工具简单介绍
前端·打包工具