admin 管理员组文章数量: 1087580
微信小程序预览 word、excel、ppt、pdf 等文件
目录- 微信小程序预览 word、excel、ppt、pdf 等文件
- 预览效果
- 前言
- 注意点
- 实现代码
微信小程序预览 word、excel、ppt、pdf 等文件
预览效果
前言
微信官方提供了相关的 API 来预览常见文件,目前支持如下格式的文件
总结一下就是先用 wx.downloadFile
API 把文件下载下来,再用 wx.openDocument
API 把它打开
- wx.downloadFile:下载文件到本地
- wx.openDocument:新开页面打开本地文档
注意点
wx.downloadFile API 单次下载允许的最大文件为 200MB
-
网络-使用说明(域名只支持
https
(wx.uploadFile、wx.downloadFile) 和wss
(wx.connectSocket) 协议) -
可以结合 DownloadTask 相关 API 实现下载进度展示
需要在小程序后台配置 downloadFile 合法域名才能下载文件
实现代码
以预览 PDF 文件为例
- 下载文件需要一个等待过程,所以加上加载提示很有必要
const util = require('../../utils/util') // 引入封装过的加载提示Page({// 预览文件previewFile(fileLink) {if(!fileLink) {return false}util.showLoading()// 单次下载允许的最大文件为 200MBwx.downloadFile({url: fileLink, // 地址已打码,自己换个其他的地址(".pdf")success: function (res) {console.log(res, "wx.downloadFile success res")if(res.statusCode != 200) {util.hideLoadingWithErrorTips()return false}var Path = res.tempFilePath //返回的文件临时地址,用于后面打开本地预览所用wx.openDocument({filePath: Path,showMenu: true,success: function (res) {console.log('打开成功');util.hideLoading()}})},fail: function (err) {console.log(err, "wx.downloadFile fail err");util.hideLoadingWithErrorTips()}})}
})
util.js
/* 加载动画相关 */
const showLoading = (tips = '加载中...') => {wx.showNavigationBarLoading()wx.showLoading({title: tips,})
}const hideLoading = () => {wx.hideLoading()wx.hideNavigationBarLoading()
}const hideLoadingWithErrorTips = (err = '加载失败...') => {hideLoading()wx.showToast({title: err,icon: 'error',duration: 2000})
}module.exports = {showLoading: showLoading,hideLoading: hideLoading,hideLoadingWithErrorTips: hideLoadingWithErrorTips,
}
本文标签: 微信小程序预览 wordexcelpptpdf 等文件
版权声明:本文标题:微信小程序预览 word、excel、ppt、pdf 等文件 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1687331067a90383.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论