uniapp实现线上文件预览
使用uni.downloadFile、uni.previewImage、uni.openDocument实现文件预览
uni.openDocument(OBJECT)官网描述
新开页面打开文档,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx。
平台差异说明
App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 字节跳动小程序、飞书小程序 | QQ小程序 | 快手小程序 |
---|---|---|---|---|---|---|---|
√ | x | √ | √ | √ | √ | √ | x |
OBJECT 参数说明:
参数名 | 类型 | 必填 | 说明 | 平台差异说明 |
---|---|---|---|---|
filePath | String | 是 | 文件路径,可通过 downFile 获得 | |
fileType | String | 否 | 文件类型,指定文件类型打开文件,有效值 doc, xls, ppt, pdf, docx, xlsx, pptx | 微信小程序 |
showMenu | Boolean | 否 | 右上角是否有可以转发分享的功能 | 微信小程序 |
success | String | 否 | 接口调用成功的回调函数 | |
fail | String | 否 | 接口调用失败的回调函数 | 微信小程序 |
complete | String | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
代码:
1 2 3 4 5 6 7 8 9 | uni.downloadFile({ url: this .sjuAjax.fileUrl+fileUrl, success: function (res) {<br> if (res.tempFilePath.<code class = "copyable" lang= "bash" >indexOf( ".png" || ".jpg" ) > -1</code>)<br> {<br> var filePath = [] <br> filePath[0]=res.tempFilePath; <br> // 预览图片 <br> uni.previewImage({ <br> urls: filePath, <br> success: function (res) { <br> console.log('打开成功') <br> }, <br> fail:function(res){ <br> console.log(res) <br> } <br> })<br> }<br> else<br> {<br> var filePath = res.tempFilePath;<br> uni.openDocument({<br> filePath: filePath,<br> success: function (res) {<br> console.log('打开文档成功');<br> }<br> });<br> }<br> }, fail: function (res){ console.log(res) } }); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」