一、H5用以下方式即可實(shí)現(xiàn):
(釘釘小程序官方目前沒有預(yù)覽附件的API,也可用這種方法實(shí)現(xiàn))
doc/docx、xls/xlsx、ppt/pptx可直接用以下鏈接打開:
注意:使用此方法,附件鏈接必須是域名。
https://view.officeapps.live.com/op/view.aspx?src= + 文檔url
let url = "https://view.officeapps.live.com/op/view.aspx?src=" + 文檔url;
//兩種方式
window.open(url); //新建窗口打開鏈接預(yù)覽
window.location.href = url; //本頁面內(nèi)跳轉(zhuǎn)鏈接實(shí)現(xiàn)預(yù)覽
pdf類型附件需要另外處理,具體方法可參考我的另一篇文章:
https://blog.csdn.net/weixin_42284117/article/details/120354159文章來源:http://www.zghlxwxcb.cn/news/detail-601504.html
二、微信小程序預(yù)覽附件用wx.downLoadFile和wx.openDocument,即可實(shí)現(xiàn):
//需要用到兩個(gè)api:wx.downLoadFile和wx.openDocument,先將附件轉(zhuǎn)換成本地連接,再用openDocument打開
wx.downloadFile({
// 示例 url,并非真實(shí)存在
url: 'http://example.com/somefile.pdf',
success: function (res) {
const filePath = res.tempFilePath
wx.openDocument({
filePath: filePath,
success: function (res) {
console.log('打開文檔成功')
}
})
}
})
具體可參考官方文檔
https://developers.weixin.qq.com/miniprogram/dev/api/file/wx.openDocument.html文章來源地址http://www.zghlxwxcb.cn/news/detail-601504.html
有更好、更專業(yè)的方法,歡迎留言評(píng)論,感謝大佬
到了這里,關(guān)于H5實(shí)現(xiàn)附件預(yù)覽功能(doc/docx、xls/xlsx、ppt/pptx、pdf)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!